MYSQL How to "Join" two Queries
I have 2 tables, one containing the UID of users with a FID (File ID) and one table containg the FID with their URI. I need to get the URI with just the UID.
My first though is to first load all of the FID with a Query and then use the fetched data to make an other 开发者_运维百科query that will load the URI in the second table according to the fetched FIDs.
Since this query is going to be run a lot on this site i was wondering what would be the most efficient way to do so ? Would it be possible to join those 2 queries ?
What you want is a simple join like this:
SELECT uid, uri FROM table1, table2 WHERE table1.fid = table2.fid
SELECT
U.UID,
F.FID,
F.URI
FROM users_Table AS U
JOIN files_Table AS F
ON U.FID = F.FID
WHERE U.UID = "user_identification_number"
It's pretty easy for SQL join.
SELECT `t1`.*, `t2`.`uri`
FROM `users` t1
JOIN `files` t2 ON `t1`.`id` = `t2`.`uid`
If I understood you question correct this could be solved with a simple JOIN
SELECT UID, URI
FROM `table1` as t1, `table2` as t2
WHERE t1.FID = t2.FID;
Read more about how joins work at http://dev.mysql.com/doc/refman/5.0/en/join.html
精彩评论