我有两张桌子。产品和产品组。 我用
"SELECT * FROM `product` `t`
WHERE (name LIKE '%test%' OR ean LIKE '%test%')
AND closed=0 "
搜索产品。现在我有另一个名为productgroups
的表。每个产品都有自己的productgroup_id
。我只需要显示那些拥有productgroup_id.closed=0
的产品。
如果productgroup.closed = 1
它不应该显示它。
我该怎么做?
答案 0 :(得分:3)
SELECT p.* FROM product p
INNER JOIN productgroup pg ON (pg.id = p.productgroup_id)
WHERE (p.name LIKE '%test%' OR p.ean LIKE '%test%')
AND p.closed=0
AND pg.closed=0
答案 1 :(得分:1)
也许我错过了问题的复杂性,但是......
SELECT * FROM
products INNER JOIN
productgroups ON product.productGroup_id = productgroups.Id
WHERE
(products.name LIKE '%test%' OR products.ean LIKE '%test%') AND products.closed=0 and productgroups.closed = 0
应该做的伎俩
答案 2 :(得分:1)
只需使用inner join:
SELECT
*
FROM
product t
INNER JOIN
productgroups g ON t.productgroup_id = g.id
WHERE
(t.name LIKE '%test%' OR t.ean LIKE '%test%')
AND
g.closed = 0
答案 3 :(得分:0)
SELECT * FROM product t
INNER JOIN productgroup pg ON t.productgroup_id = pg.id
WHERE (t.name LIKE '%test%' OR t.ean LIKE '%test%') AND t.closed=0 AND pg.Closed = 0