mysql query - accessing field without affecting output
I have 2 tables in mysql: tableA and tableB
tableA: idA
tableB: idB, name
Now i want to fire following query:
SELECT * FROM tableA WHERE idA in (1, 2, 3, 4);
idA and idB are exactly same. I want to access "name" 开发者_如何学Pythonfield of tableB in the above query.
How do i do this???
Thanks alot for help Regards
Use a MySQL JOIN
SELECT tableB.name
FROM tableA
INNER JOIN tableB ON tableA.idA=tableB.idB
WHERE idA in (1, 2, 3, 4);
Why select from TableA if you only need the id which is already in tableB?
SELECT idB, name FROM tableB where idB in (1, 2, 3, 4)
If you need to refer to the id field as idA, you can write
SELECT idB as idA, name FROM tableB where idB in (1, 2, 3, 4)
Thanks alot for help...
I just forgot the basic thing (thanks @yc you reminded me):
SELECT idA, name FROM tableA, tableB WHERE idA in (1, 2, 3, 4) and idA = idB;
Regards
精彩评论