mysql联合查询与每个联合上的单独别名

时间:2018-05-22 18:45:43

标签: mysql union alias

创建搜索查询mysql,使用UNION加入多个表,现在我的问题是添加别名以识别每个表,因为如果我添加带有别名的表名,它将显示错误COLUMN NOT FOUND我试图在每个联盟表上添加别名,但是我遇到了错误,我不知道该怎么办才能让它写出来,任何人都可以提供帮助。

QUERY

SELECT * FROM (
    (
        SELECT 'food' as type,
        food_id AS item_id,
        food_name AS item_name,
        primary_image AS item_img,
        food_price AS item_price,
        food_currency AS item_currency,
        food_discount AS item_discount,
        chef_status AS ison
        FROM foods
    ) AS f

    UNION

    (
        SELECT 'chef' as type,
        chef_pagename AS item_id,
        business_name AS item_name,
        chef_photo AS item_img,
        NULL AS item_price,
        NULL AS item_currency,
        NULL AS item_discount,
        chef_status AS ison
        FROM our_chefs
    ) AS c
) AS fsc

WHERE f.food_name LIKE '%chicken%'
OR f.item_price LIKE '%100%'
OR c.business_name LIKE '%chicken%'
AND fsc.ison = 1

谢谢你们的贡献,我现在通过

解决了问题
WHERE item_name LIKE '%chicken%'
OR item_price LIKE '%100%'
AND fsc.ison = 1

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的:

SELECT * FROM (

  (
    SELECT 'food' as type,
    food_id AS item_id,
    food_name AS item_name,
    primary_image AS item_img,
    food_price AS item_price,
    food_currency AS item_currency,
    food_discount AS item_discount,
    chef_status AS ison
    FROM foods
    WHERE food_name LIKE '%chicken%'
    OR item_price LIKE '%100%'
  )  

  UNION

  (
    SELECT 'chef' as type,
    chef_pagename AS item_id,
    business_name AS item_name,
    chef_photo AS item_img,
    NULL AS item_price,
    NULL AS item_currency,
    NULL AS item_discount,
    chef_status AS ison
    FROM our_chefs
    WHERE business_name LIKE '%chicken%'
  )  
) AS a

WHERE  a.ison = 1