我试图从两个表中得到结果,并在php中进行以下查询:
r2 = $con->query('
(
SELECT *
FROM pat
WHERE pfn LIKE "%'.$search_string.'%"
OR pfln LIKE "%'.$search_string.'%"
) UNION all (
SELECT *
FROM patfam
WHERE fn LIKE "%'.$search_string.'%"
OR ln LIKE "%'.$search_string.'%"
)'
);
但$ r2中没有结果集,它返回FALSE。
我可以使用两个select语句获得结果但是使用union我得到了错误
所以我在这里错过了什么?
谢谢
答案 0 :(得分:0)
一种方法是命名您在两个表中选择的列,如果一个表的列数多于另一个表,则填充空的列...示例:
SELECT id AS id, name AS name, column3 AS column3, 'table1' as table_name FROM table1
UNION
SELECT id_table2 AS id, fname AS name, '' AS column3, 'table2' as table_name FROM table2