简体   繁体   中英

MySQL statement to read data from one table with checks on another table

I have these two tables:

Achievement :

成就

Achieves :

实现

Question :

I want to retrieve rows from table Achievement. But, I do not want all the rows, I want the rows that a specific Steam ID has acquired. Let's take STEAM_0:0:46481449 for example, I want to check first the list of IDs that STEAM_0:0:46481449 has acquired (4th column in Achieves table states whether achievement is acquired or not) and then read only those achievements.

I hope that made sense, if not let me know so I can explain a little better.

I know how to do this with two MySQL statements, but can this be done with a single MySQL statement? That would be awesome if so please tell me :D

EDIT: I will add the two queries below

SELECT * FROM Achieves WHERE Achieves.SteamID = 'STEAM_0:0:46481449' AND Achieves.Acquired = 1;

Then after that I do the following query

SELECT * FROM Achievement;

And then through PHP I would check the IDs that I should take and output those. That's why I wanted to get the same result in 1 query since it's more readable and easier.

In sql left join , applying conditions on second table will filter the result when join conditions doesn't matter:

   Select * from achievement 
   left join achieves on (achievement.id=achieves.id) 
   where achieves.acquired=1 and achieves.SteamID = 'STEAM_0:0:46481449'

Besides,I suggest not using ID in the achieves table as the shared key between two tables. Name it something else.

I don't think a left join makes sense here. There is no case where you don't want to see the Achievement table.

Something like this

 SELECT * 
 FROM Achieves A
 JOIN Achievement B on A.ID = B.ID
 WHERE A.SteamID = 'STEAM_0:0:46481449' 
   AND A.Acquired = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM