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ステートメントの結果を1つの結果セットに結合するために使用されます。