SQL:如何从两个表中获取关系数据? (使用JOIN吗?) [英] SQL: How to get relational data from two tables? (Using JOIN ?)

查看:86
本文介绍了SQL:如何从两个表中获取关系数据? (使用JOIN吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在尝试进行一些交叉关系SQL查询,但没有成功.想知道是否有人可以提供帮助?

I have been experimenting with doing some cross relational SQL queries, without success so far. Wondering if anyone can help?

我有两个感兴趣的表,正在尝试找出我的哪些客户(=公司联系人1)必须处理未完成的项目.这是桌子的结构:

I have two tables of interest and am trying to find out which of my customers (=company contact 1) have to do items outstanding. Here's the table sturcture:

TABLE: CUSTOMERS

ID    |    CUSTOMER_NAME    |    COMPANY_CONTACT
=================================================
1           Bob                     1
2           Billy                   2
3           Susan                   2
4           Ryan                    3
5           Sally                   1

.

TABLE: TO_DOS
ID    |    TASK         |    CUSTOMER_ID
============================================
1          Make Tea                 5
2          Pick flowers             1
3          Do invoices              3
4          Tidy up                  4
5          Drive van                2

理想的输出应该是

CUSTOMER_ID     |   CUSTOMER_NAME  |  TASK
=================================================================
5           Sally         Make Tea
1           Bob           Pick flowers

到目前为止,我已经尝试过了,但是却无济于事:

So far I've tried this but it's not getting me anywhere:

SELECT * FROM TO_DOS
INNER JOIN CUSTOMERS
WHERE CUSTOMERS.COMPANY_CONTACT=1

任何想法都将不胜感激!

Any ideas greatly appreciated!

推荐答案

SELECT CUSTOMERS.ID, CUSTOMERS.CUSTOMER_NAME, TO-DOS.TASK 
FROM TO_DOS
INNER JOIN CUSTOMERS 
ON TO_DOS.CUSTOMER_ID = CUSTOMERS.ID
WHERE CUSTOMERS.COMPANY_CONTACT = 1

这篇关于SQL:如何从两个表中获取关系数据? (使用JOIN吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆