我不知道我的SQL连接有什么问题,这是我第一次使用它

Ok, I have this code, I dont have error.... i have just nothing.... nothing apears:

$idea = $bdd->query("SELECT * FROM ideas 
INNER JOIN follow ON ideas.idcreador=follow.idseguidor 
WHERE follow.idseguidor ='".$_SESSION['userid']."' ORDER BY id DESC");

while($datoideaperfil2 = $idea->fetch())
{
  echo $datoideaperfil2['ideas.idcreador'] <br />;
}

What is wrong? Help me please its my fist time with SQL Joins...

Thanks

you have used a INNER JOIN which will only display data if both tables have corresponding rows. Try using a LEFT or RIGHT join.

There doesn't appear to be anything wrong with the SQL itself, but one thing that I do suspect is that you have a column called id in both tables. If you don't use an alias, then mysql won't know what to order by and return an error.

Try this:

SELECT 
    * 
FROM 
    ideas 
        INNER JOIN follow 
            ON ideas.idcreador=follow.idseguidor 
WHERE 
    follow.idseguidor ='".$_SESSION['userid']."' 
ORDER BY    
    follow.id DESC

To understand what's wrong with the query you need to understand how joins in SQL work. A JOIN results in a "table" where for every row in table A, you get a resulting row that is combined with every row in table B. So if table A has 5 rows, and table B has 10 rows, you get 50 (5x10) rows, which you need to filter with your WHERE clause.

Results also differ, and in your case this is the important part, wether you go for an INNER JOIN or an OUTER JOIN. An INNER JOIN's resulting "table", ONLY contains rows where the rows from table A and table B match the ON clause. So if you have a row in table ideas, which has no relation to any rows in table follow, it won't show up in the results. Would you choose an OUTER JOIN (a LEFT or RIGHT OUTER JOIN) any non-matching row will show up, but the row's values will be set to NULL for whatever values it could not find a relation for.

Since you chose an INNER JOIN, and you get no results, I'm guessing you have no relations between any rows.

What's also important is that you specify what table you want the ORDER BY clause to be ordered by, or you get an ambigious column exception (if both tables have a column named id).