我在为三个表中的uninon指定select语句中的条件时遇到问题。这是我的代码
create table master_data(
select id,number,product_id
from tablename_A
where id REGEXP '^[0-9]*$' and id != ' ' and product_id != 0 and product_id is not null
union
select id,number,product_id
from tablename_B
where id REGEXP '^[0-9]*$' and id != ' ' and product_id != 0 and product_id is not null
union
select id,number,product_id
from tablename_C
where id REGEXP '^[0-9]*$' and id != ' ' and product_id != 0 and product_id is not null
);
我收到此错误:
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 'id!=''和product_id!= 0附近,而product_id不是 nullunionselect id'在第8行
答案 0 :(得分:1)
首先,什么是' REGEXP' ?
我更喜欢这个:
WITH master_data AS
(
select
id,
number,
product_id
from
tablename_A
union
select
id,
number,
product_id
from
tablename_B
union
select
id,
number,
product_id
from
tablename_C
)
SELECT
*
FROM
master_data
WHERE
id NOT IN '^[0-9]*$'
and id != ' '
and product_id != 0
and product_id is not null
答案 1 :(得分:0)
以下在mysql 5.6.31(Microsoft Server)和mysql 5.7.14(Redhat Linux)上运行良好
create table tablename_A
( id int not null,
product_id int not null,
number int not null
);
create table tablename_B
( id int not null,
product_id int not null,
number int not null
);
create table tablename_C
( id int not null,
product_id int not null,
number int not null
);
create table master_data as
select id,number,product_id
from tablename_A
where (id REGEXP '^[0-9]*$') and id != ' ' and product_id != 0 and product_id is not null
union
select id,number,product_id
from tablename_B
where (id REGEXP '^[0-9]*$') and id != ' ' and product_id != 0 and product_id is not null
union
select id,number,product_id
from tablename_C
where id REGEXP '^[0-9]*$' and id != ' ' and product_id != 0 and product_id is not null;