我需要在18个单独的SQL表中搜索字符串。搜索将查找不同命名的字段和某些表上的多个字段。
虽然我可以使用下面的内容(前两个表)来实现这一点,但是我觉得一旦所有18个表加入,代码就变得非常笨拙。
有什么方法可以改进这段代码吗?
同样,当给出结果时,我可以找到PHP的答案来自哪个表?
mysql_query("SELECT a.about,b.title,b.article,b.description
FROM about a
JOIN articles b ON b.user=a.user
WHERE (upper(a.about) LIKE'%".$val."%') OR (upper(b.title) LIKE'%".$val."%') OR (upper(b.article) LIKE'%".$val."%') OR (upper(b.descirption) LIKE'%".$val."%') ORDER BY $order $max");
答案 0 :(得分:1)
使用UNION
SELECT about from some WHERE about like '%some%'
UNION
SELECT about from some_other WHERE about like '%some%'
如果您想允许重复结果,请使用UNION ALL
答案 1 :(得分:0)
查看UNION
操作。
select x from table_1 where blabla
union
select y from table_2 where blabla
等等。
x
和y
必须采用相同的格式。