我可以在mysql中合并2选择吗?

时间:2017-01-29 20:22:55

标签: mysql sql

我有2个查询

table1:item:id/title

table:2 tag:id/item_id/name

1-

   SELECT `*`
   FROM `item`
   INNER JOIN `tag` ON `item`.`id` = `tag`.`item_id`
   WHERE
   `tag`.`name` = 100;

2 -

   SELECT `*`
   FROM `item`
   WHERE
   `title` = 100;

我如何加入它们(没有重复的item行)?

3 个答案:

答案 0 :(得分:0)

使用OR修饰符

SELECT * FROM item INNER JOIN tag ON item.id = tag.item_id WHERE tag.name = 100 OR title=100;

答案 1 :(得分:0)

也许你打算这样做:

select i.*
from item i
where i.title = 100 or
      exists (select 1 from tag t where i.id = t.item_id and t.name = 100);

这会返回标题为100或标记为100的所有项目。

答案 2 :(得分:0)

table1:item:id / title table:2 tag:id / item_id / name

Christophers-MacBook-Pro:~ cccmusicality$ virtualenv env
Using base prefix '/Applications/anaconda'
New python executable in /Users/cccmusicality/env/bin/python
ERROR: The executable /Users/cccmusicality/env/bin/python is not functioning
ERROR: It thinks sys.prefix is '/Users/cccmusicality' (should be '/Users/cccmusicality/env')
ERROR: virtualenv is not compatible with this system or executable

这将显示两个标签名称为100,项目标题为100。 用于选择所有列的SELECT SELECT * FROM item JOIN tag ON item.id = tag.item_id WHERE tag.name = 100 OR item.title= 100; 也不应该用单引号括起来。它只是写成SELECT *