比较两个查询laravel的结果

时间:2015-08-17 22:10:04

标签: php mysql sql laravel

我有两张如下表:

cat

+-------------------------------+
|   id | 1  | 2  | 3  | 4  | 5  |
+-------------------------------+
| name | Hi | Ho | Hu | Ha | He |
+-------------------------------+

选择了猫

+----------------+
|   id | 2 | 5   |
+----------------+
| name | Ho | He |
+----------------+

预期输出:

1 - > No
2 - > Yes
3 - > No
4 - > No
5 - > Yes

1 个答案:

答案 0 :(得分:0)

select id,
case when id in (select distinct id from selected_cat) then 'Yes'
else 'No' end
as somecol
from cat;

您可以使用case语句检查另一个表中是否存在id。