合并两个SELECT查询 [英] merging two SELECT queries
本文介绍了合并两个SELECT查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要么变老,要么我需要编写的查询变得越来越复杂.以下查询将获取与用户关联的所有tasks
.
I am either getting old or the queries that I need to write are getting more and more complicated. The following query will get all the tasks
associated with the user.
"SELECT `date`
FROM `tasks`
WHERE `user_id`= 1;"
tasks
表为(id
,date
,user_id
,url_id
);
现在,我还需要获取url_id
与用户通过
Now, I need to get as well records that url_id
associates with the user trough the
`urls` table (`id`, `user_id`)
独立查询如下:
"SELECT `t1`.`data`
FROM `tasks` `t1`
JOIN `urls` `u1` ON `u1`.`id` = `t1`.`url_id`
WHERE `u1`.user_id` = 1;"
但是,是否可以将这两个查询合并为一个查询?我的逻辑是应该这样,尽管我不知道如何进行实际的JOIN.
Though, is it possible to merge these two queries into a single query? My logic says it should be, though I don't see how to do the actual JOIN.
推荐答案
我可能会使用联盟.
SELECT `date`
FROM `tasks` WHERE `user_id`=1
UNION
SELECT `t1`.`date`
FROM `tasks` `t1`
INNER JOIN `urls` `u1` ON `u1`.`id` = `t1`.`url_id`
WHERE `u1`.user_id`=1;
这篇关于合并两个SELECT查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文