SQL - 从一个表中获取另一个表中不“LIKE”的记录

时间:2014-07-25 21:49:59

标签: mysql sql

我有两张桌子: 表格:产品帖子

产品具有字段型号,其值为“AH0002”,“O-PO-201”,“O-PO-304”等。 表帖子有字段 post_title ,其值为“产品AH0002最佳”,“红色O-PO-201非常好”。

我需要的是显示其名称未出现在posts表中的产品行(在post_title字段中)。

如何在mysql中执行此操作?

3 个答案:

答案 0 :(得分:1)

尝试:

select pr.*
  from products pr
  left join posts po
    on post_title like concat('%', pr.model, '%')
 where post_title is null

以上假设是mysql语法,它会因数据库而略有不同。

SQL Fiddle

答案 1 :(得分:1)

我想你想要这样的东西:

SELECT p.*
  FROM products p
 WHERE NOT EXISTS (SELECT 1 FROM posts WHERE post_title LIKE '%' + p.model + '%')

此语法假定为Microsoft SQL Server。

对于MySQL,只需将LIKE子句更改为LIKE CONCAT('%',p.model,'%')

答案 2 :(得分:0)

使用LIKE

select *
from products A
where model not in (
   select model
   from posts
   inner join products
      on post_title like concat('%',model,'%')
) 

查看SQL Fiddle

相关问题