开发者

MySQL query with left Join?

I have two tables: Items:

| Item|
   A
   B
   C

userItems:

| UID |  Item
   1      A
   1      C

This loop shows the data from table Items:

$data = mysql_query("SELECT * FROM `Items`ORDER by `Icons` ASC"); 

while($row = mysql_fetch_array( $data )) 
    {
    echo $row['Item'];
    echo "Unlock";
    }

Basically I need to display Unlocked instead of Unlock if the user with UID = $uid has the item.

This query gets the user items:

$data = mysql_query("SELECT * FROM `userItems` WHERE `UID` = '$uid'"); 

I believe the solution is an left join, but I don't know how to do it. How can I开发者_如何学编程 make it work?


SELECT Items.*, useritems.UID FROM `Items` 
    left join useritems on Items.Item=useritems.Item 
    ORDER by `Icons` ASC

Then just use logic in your PHP to display Unlocked when user $row["items.UID"] equals $uid

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜