SELECT some_primary_id as id, 'fusers' as table_name FROM fusers WHERE email = '$email' UNION
SELECT some_other_primary_id as id, 'tusers' as table_name FROM tusers WHERE email = '$email' UNION
SELECT some_misc_primary_id as id, 'lusers' as table_name FROM lusers WHERE email = '$email'
注意我使用fusers_id as id,因为联合选择中的所有列都需要具有相同的名称
感谢@njk,您可以使用UNION ALL,因为UNION 单独检查重复行并删除它们会更快。
语法:
(SELECT a as COMMON_COLUMN_1, b as COMMON_COLUMN_2, c as COMMON_COLUMN3 FROM xxx WHERE ...)
UNION
(SELECT d as COMMON_COLUMN_1, e as COMMON_COLUMN_2, f as COMMON_COLUMN3 FROM yyy WHERE ...)
UNION
(SELECT g as COMMON_COLUMN_1, h as COMMON_COLUMN_2, i as COMMON_COLUMN3 FROM zzz WHERE ...)
# where a,b,c are columns from table xxx ONLY
# where d,e,f are columns from table yyy ONLY
# where g,h,j are columns from table zzz ONLY
只需UNION 用于将多个 SELECT 语句的结果组合成一个结果集。