P粉5390555262023-08-11 10:16:00
要在一組固定的表中搜索,您可以使用 UNION
子句組合各個查詢,然後添加一個硬編碼的字串來標識來源:
SELECT packaging_id AS product_id, 'packaging' AS table_name FROM packaging WHERE name = 'Cardboard box' UNION ALL SELECT stationery_id, 'stationery' FROM stationery WHERE description = 'Cardboard box' UNION ALL SELECT sales_id, 'sales' FROM sales WHERE offer = 'Cardboard box'
您需要確保結果集中每個列具有相同的資料類型,並且您可以使用第一個語句為結果集設定一個規範化的列名。