我正在尝试为具有与请求类似的标题或子类别名称的文章获取结果。
Table 1
Subcategory_ID Subcategory_Name
Table 2
Article_ID Title Subcategory_ID
现在我需要从表2中获取文章,其中title或subcategory_names与我们正在寻找的内容相匹配。
这是我所拥有的,但我无法想出其余的
SELECT table1.*, table2.* FROM table1,table2 WHERE table2.title = '%keyword%'
其余的我搞砸了......
解决方案是什么?
答案 0 :(得分:2)
试试这个:
SELECT t2.Title, t1.Subcategory_Name
FROM Table2 t2 JOIN Table1 t1
ON t2.Subcategory_ID = t1.Subcategory_ID
WHERE t2.title LIKE '%keyword%' OR t1.Subcategory_Name LIKE '%keyword%'
答案 1 :(得分:1)
SELECT table1.*, table2.* FROM table1, table2
WHERE table1.Subcategory_ID=table2.Subcategory_ID
AND (table2.title LIKE '%keyword%' OR table1.Subcategory_Name LIKE '%keyword%')
使用MySQL INNER JOIN,如果在WHERE子句中使用“table1.Subcategory_ID = table2.Subcategory_ID”,则它是隐式的。
答案 2 :(得分:0)
SELECT table1.*, table2.* FROM table1 INNER JOIN table2 USING (Subcategory_id)
WHERE table2.title LIKE '%keyword%'
您可以将USING (Subcategory_id)
替换为ON (table1.subcategory_id = table2.subcategory_id)
。
有关详细说明,请参阅 MySQL Tutorial - Join 。
答案 3 :(得分:0)
可能是
select t1.*,t2.*
from `table2` t2 join `table1` t1
on t1.Subcategory_ID = t2.Subcategory_ID
where t2.title LIKE '%keyword%' or t1.Subcategory_Name LIKE '%keyword%'