Thanks for reading my question. I am trying to connect the 3 columns of multiple tables together then find the nameProduct column that match LIKE request. All tables have different columns and look just like 3 columns: name, id and rate. This is my code:
(
    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'
;
And MySQL was report:
#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
What am I doing wrong? Please help me
Advertisement
Answer
You can’t apply WHERE to the result of UNION directly. You can filter queries for each subselect or wrap result of UNION in SELECT:
SELECT *
  FROM (
      (
      SELECT
        nameProduct,
        rate,
        idProduct
      FROM
        tblaptoplist 
      )
      UNION ...
  ) AS r
 WHERE r.nameProduct LIKE 'Asus'