简体   繁体   中英

Selecting one record from two tables

I'm stuck with one SQL query. I have two tables:

  1. users

    ________________________ | id | company | worker| ------------------------- | 1 | my comp | John |
  2. tasks

    _________________________ | id | name | company | ------------------------- | 1 | exm | my comp |

My problem is that I want to show tasks of these companies which worker is John. I'm in trouble in that for hours but I don't know how to do it. Is there any SQL query to do that?

You can do a simple join using company column from both tables and use where clause to filter results for John

SELECT t.*
FROM users u
JOIN tasks t USING(company)
WHERE u.worker ='John'

You want use the inner join tag. Just a modification on the other queries mentioned for better clarity.

SELECT task.name,user.worker,user.company 
FROM tasks as task INNER JOIN users as user 
 ON user.company=task.company 
 WHERE user.worker='John';

你可以做一个简单的连接,比如......

$qry = "SELECT u.id,u.company,u.worker,t.id,t.name,t.company FROM users as u JOIN tasks as t ON u.company = t.company WHERE u.worker = 'John'";

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