开发者

MySQL query with left Join?

开发者 https://www.devze.com 2023-04-03 04:31 出处:网络
I have two tables: Items: Item| A B C userItems: UID |Item 1A 1C This loop shows the data from table Items:

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

精彩评论

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