mysql 三表选一

发布于 2024-12-03 03:23:25 字数 1226 浏览 0 评论 0原文

我有以下表格:

| table_one | table_two | table_three | table_four |
| t_one_id  | t_two_id  | t_three_id  | t_four_id  | // primary key (auto increment)
|           | two_nid   | three_nid   | four_nid   | // foreign key to table_one.t_one_id
| tone_date | ttwo_date | tthree_date | tfour_date | // time this was posted.
// other fields

以及以下查询,

SELECT `table_two` . * ,  `table_three` . * ,  `table_four` . * 
FROM  `table_two` 
JOIN ( `table_three` ) ON `table_three`.`three_nid`=1
JOIN ( `table_four`  ) ON  table_four`.`four_nid`=1
WHERE  `table_two`.`two_nid`=1

以及以下数据:

table_one: 1, 11/08/2011
table_two: 1, 1, 12/08/2011
table_three: 1, 1, 13/08/2011
table_four: 1, 1, 14/08/2011

现在我的查询不断返回零结果,尽管但我正在寻找它返回的是(当在每个表的日期订购时):

result 1: 1 | 1 | 12/08/2011 | // this is from table_two
result 2: 1 | 1 | 13/08/2011 | // from table three
result 2: 1 | 1 | 14/08/2011 | // from four

我也尝试过以下方法但没有成功:

SELECT * FROM table_two t2, table_three t3, table_four t4 WHERE t2.two_nid = 1 OR t3.three_nid=1 OR t4.four_nid=1;

我的 SQL 查询哪里出了问题?

提前致谢,如果需要更多信息,请随时询问。

I've got the following tables:

| table_one | table_two | table_three | table_four |
| t_one_id  | t_two_id  | t_three_id  | t_four_id  | // primary key (auto increment)
|           | two_nid   | three_nid   | four_nid   | // foreign key to table_one.t_one_id
| tone_date | ttwo_date | tthree_date | tfour_date | // time this was posted.
// other fields

And the following query,

SELECT `table_two` . * ,  `table_three` . * ,  `table_four` . * 
FROM  `table_two` 
JOIN ( `table_three` ) ON `table_three`.`three_nid`=1
JOIN ( `table_four`  ) ON  table_four`.`four_nid`=1
WHERE  `table_two`.`two_nid`=1

And the following data:

table_one: 1, 11/08/2011
table_two: 1, 1, 12/08/2011
table_three: 1, 1, 13/08/2011
table_four: 1, 1, 14/08/2011

Now my query keeps returning zero results, even though but what I am looking for it to return is (when ordered on the date of each table):

result 1: 1 | 1 | 12/08/2011 | // this is from table_two
result 2: 1 | 1 | 13/08/2011 | // from table three
result 2: 1 | 1 | 14/08/2011 | // from four

I have also tried the following but without success:

SELECT * FROM table_two t2, table_three t3, table_four t4 WHERE t2.two_nid = 1 OR t3.three_nid=1 OR t4.four_nid=1;

Where am I going wrong with my SQL queries?

Thanks in advance, if more info needed, don't hesitate to ask.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖伴 2024-12-10 03:23:25

如果要将每个表的结果显示为单独的行,则需要进行三个单独的选择,并对结果进行 UNION。试试这个:

SELECT `table_two` . * 
FROM  `table_one` 
JOIN ( `table_two` ) ON `table_two`.`two_nid`=`table_one`.`t_one_id`
WHERE  `table_one`.`t_one_id`=1
UNION
SELECT `table_three` . * 
FROM  `table_one` 
JOIN ( `table_three` ) ON `table_three`.`three_nid`=`table_one`.`t_one_id`
WHERE  `table_one`.`t_one_id`=1
UNION
SELECT `table_four` . * 
FROM  `table_one` 
JOIN ( `table_four` ) ON `table_four`.`four_nid`=`table_one`.`t_one_id`
WHERE  `table_one`.`t_one_id`=1

If you want to display the results from each table as a seperate row, you'll need to have three seperate selects, and UNION the results. Try this:

SELECT `table_two` . * 
FROM  `table_one` 
JOIN ( `table_two` ) ON `table_two`.`two_nid`=`table_one`.`t_one_id`
WHERE  `table_one`.`t_one_id`=1
UNION
SELECT `table_three` . * 
FROM  `table_one` 
JOIN ( `table_three` ) ON `table_three`.`three_nid`=`table_one`.`t_one_id`
WHERE  `table_one`.`t_one_id`=1
UNION
SELECT `table_four` . * 
FROM  `table_one` 
JOIN ( `table_four` ) ON `table_four`.`four_nid`=`table_one`.`t_one_id`
WHERE  `table_one`.`t_one_id`=1
留蓝 2024-12-10 03:23:25
SELECT * FROM table1
JOIN table2 ON table1.one_nid=table2.two_nid
JOIN table3 ON table1.one_nid=table3.three_nid
WHERE table1.one_nid=1

...这当然只有在保证 table1 中的每一行在其他每个表中都有一个具有相同 id 的对应行时才有效。否则,您应该阅读有关 LEFT JOIN 的内容。

SELECT * FROM table1
JOIN table2 ON table1.one_nid=table2.two_nid
JOIN table3 ON table1.one_nid=table3.three_nid
WHERE table1.one_nid=1

... this of course would only work if it's guaranteed that each row in table1 has a corresponding row with the same id in each of the other tables. Otherwise, you should read about LEFT JOINs.

养猫人 2024-12-10 03:23:25

您是否想“合并”结果?
如果是这样,请检查 UNION 关键字。

Are you trying to "combine" the results?
If so, check UNION keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文