mysql并集和类似的多表#1064 mysql数据库

lx0bsm1f  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(335)

谢谢你看我的问题。我正在尝试将多个表的3列连接在一起,然后找到与like请求匹配的nameproduct列。所有表都有不同的列,看起来就像3列:name、id和rate。这是我的密码:

(
    SELECT
        nameProduct,
        rate,
        idProduct
    FROM
        tblaptoplist 

)
UNION
    (
    SELECT
        nameProduct,
        rate,
        idProduct
    FROM
        tbcpulist 
)
UNION
    (
    SELECT
        nameProduct,
        rate,
        idProduct
    FROM
        tbgraphicslist
)
UNION
    (
    SELECT
        nameProduct,
        rate,
        idProduct
    FROM
        tbpccaselist 
)
UNION
    (
    SELECT
        nameProduct,
        rate,
        idProduct
    FROM
        tbradiatorslist 
)
UNION
    (
    SELECT
        nameProduct,
        rate,
        idProduct
    FROM
        tbramlist 

)WHERE nameProduct LIKE 'Asus'
;

mysql被报道:


# 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE

    nameProduct LIKE 'Asus' LIMIT 0, 25' at line 54

我做错什么了?请帮帮我

vsaztqbk

vsaztqbk1#

你不能申请 WHERE 结果 UNION 直接。您可以在“选择”中为每个子选择或并集的换行结果筛选查询:

SELECT *
  FROM (
      (
      SELECT
        nameProduct,
        rate,
        idProduct
      FROM
        tblaptoplist 
      )
      UNION ...
  ) AS r
 WHERE r.nameProduct LIKE 'Asus'

相关问题