I am performing MySQL functions using PHP for my webpages.
I have got a table
table name -> users
uname oname
john alex
john murray
john joseph
john ray
and another table
table name -> more_info
fname lname more
alex some name some info
murray // //
joseph // //
ray // //
What I am trying to do is to run a query
Retrieve all the oname
column values which are matched with uname
john
This statement successfully retrieves the oname column values if uname = john
SELECT oname FROM users WHERE uname = 'john '
Now I want a statement something like this, based on the previously retrieved oname column values
SELECT * FROM more_info WHERE fname=oname // previos ones
I guess its Join
or innerJoin
but somehow I am unable to get the correct syntax of it or any other simple query to do this.
My approach
I thought to retrieve the oname column values first and then inside a for loop, based on number of rows returned run a query for each column, but this approach eats up my server.
You can merge your two queries into one by a simple INNER JOIN
SELECT * FROM more_info mi
INNER JOIN users u ON mi.fname = u.oname
WHERE u.uname = 'john'
If there is some chance of null values in more_info
table, then you should use LEFT JOIN
instead of INNER JOIN
. Otherwise it is not necessary.
试试这个查询,
SELECT * FROM `more_info` `i` INNER JOIN `users` `u` ON `i`.`fname`=`u`.`oname` WHERE `u`.`uname`="john"
我认为以下查询将对您有所帮助。
SELECT * FROM `more_info` WHERE `fname` = (SELECT `oname` FROM `users` WHERE `uname` = "john")
SELECT * FROM users A
INNER JOIN more_info B
ON A.oname = B.fname
WHERE uname = 'john'
See the MySQL documentation on the JOIN syntax for details.
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.