简体   繁体   English

从两个表中选择一条记录

[英]Selecting one record from two tables

I'm stuck with one SQL query.我被一个 SQL 查询困住了。 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?是否有任何 SQL 查询可以做到这一点?

You can do a simple join using company column from both tables and use where clause to filter results for John您可以使用两个表中的 company 列进行简单连接,并使用 where 子句过滤 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'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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