开发者

Which MySQL JOIN do I use?

开发者 https://www.devze.com 2023-02-26 22:29 出处:网络
SELECT comments.comment, comments.title, userBase.uID, userBase.fname FROM comments JOIN userBase WHERE comment.ID=users.ID AND commen开发者_运维百科ts.uID=\'$uID\' AND comments.title=\'$title\'
SELECT comments.comment, comments.title, userBase.uID, userBase.fname FROM comments JOIN userBase WHERE comment.ID=users.ID AND commen开发者_运维百科ts.uID='$uID' AND comments.title='$title' 

However this only gives me the information from the comments database when using:

$row = mysql_fetch_row($result);
    print_r($row);


If I understand correctly what you want, this is the query

SELECT comments.comment, comments.title, userBase.uID, userBase.fname 
FROM comments 
INNER JOIN userBase ON comment.ID=users.ID 
                    AND comments.uID='$uID' 
                    AND comments.title='$title'

Anyway there are several problems with your questions:

  • I assume comments and userBase are tables, not database
  • You have an AND just after the WHERE clause, which makes no sense at all
  • It is not clear what the relation is. Are you sure that you want to equate comments.ID with userBase.ID? In this case you are using the ID key for the foreign key in the comments, which is not very readable. You may want to use something like comments.user_id

Edit: Just to be clear, there is an alternative syntax for joins, which uses the WHERE keyword. You could rewrite the above as

SELECT comments.comment, comments.title, userBase.uID, userBase.fname 
FROM comments, userBase 
WHERE comment.ID=users.ID 
  AND comments.uID='$uID' 
  AND comments.title='$title'

but it is not the SQL standard. It works in MySQL, and I think also in SQLServer, but I'm not completely sure about the other db vendors. In any case, the problem with your query is that you were mixing the two different syntaxes.


Give this a Try:

SELECT c.comment, c.title, u.uID, u.fname
FROM comments c, userBase u
WHERE c.ID = u.ID
and c.title = '$title'
and c.uID = '$uID'

I'm assuming all your variables have been initialized and have some value. For Primary Key's you don't need a ' (quotation) around the value. Also, you need to pick whether you're using a JOIN style statement or a WHERE = style statement.

Here is more info on JOINS and WHERE = statements. http://www.w3schools.com/sql/sql_join.asp http://www.w3schools.com/sql/sql_where.asp


Inner join will give you all rows from both tables that satisfy the join condition, this join condition is specified in the on clause, (which you have omitted). In contrast a left join will give you all rows from the left table in the join (the first table), and any rows from the right table that satisfy the join condition, and the opposite is true for a right join.

I think what you are looking for in your SQL is :

SELECT comments.comment, comments.title, userBase.uID, userBase.fname 
FROM comments INNER JOIN userBase ON comment.ID=users.ID 
WHERE comments.uID='$uID' AND comments.title='$title'
0

精彩评论

暂无评论...
验证码 换一张
取 消