Select 来自 mysql 中的多个表
Select from multiple tables in mysql
是否可以直接在 phpmyadmin 中以 sql 形式一次从 10 个表中 select?
在我的数据库中,我有 10 个表:table1
、table2
、... table10
。我在其中有相同的专栏 column1
。该列中的记录始终以 123...
开头,并且长度始终为 12 个符号。
我想要做的是 select 从那些不以 123
开头的表记录中,它们的长度 <12 个符号。
这可能吗?
根据你的Q和我对这个Q的理解
select table1.column1, table2.column1, ... table10.column1 from
table1, table2, ... table10 where
table1.column1 = table2.column1 and
table2.column1 = table3.column1 and .....
table9.column1 = table10.column1 and
table1.column1 not like '123%'
and table2.column1 not like '123%'.....
and len(table1.column1) < 12, len(table2.column1) < 12....
以下查询是您要查找的内容
SELECT * from
(SELECT column1, 'table1' table_name from table1
UNION ALL
SELECT column1, 'table2' table_name from table2
UNION ALL
... --repeat for all 10 tables
SELECT column1, 'table10' table_name from table10)
where column1 not like '123%' and length(column1) < 12;
这将 return 结果如下:
column1 table_name
abc table2
1245yy table5
这将为您提供每个 table 中第 1 列不符合长度和格式条件的所有记录。
编辑:
非常感谢 Using UNION ALL
in place of UNION
is safe (and with performance boosts) because if the column1 happens to not have a unique index, then duplicate values coming from one table will be dropped. For some extra reading on the performance bonuses of using UNION ALL, check this link.
是否可以直接在 phpmyadmin 中以 sql 形式一次从 10 个表中 select?
在我的数据库中,我有 10 个表:table1
、table2
、... table10
。我在其中有相同的专栏 column1
。该列中的记录始终以 123...
开头,并且长度始终为 12 个符号。
我想要做的是 select 从那些不以 123
开头的表记录中,它们的长度 <12 个符号。
这可能吗?
根据你的Q和我对这个Q的理解
select table1.column1, table2.column1, ... table10.column1 from
table1, table2, ... table10 where
table1.column1 = table2.column1 and
table2.column1 = table3.column1 and .....
table9.column1 = table10.column1 and
table1.column1 not like '123%'
and table2.column1 not like '123%'.....
and len(table1.column1) < 12, len(table2.column1) < 12....
以下查询是您要查找的内容
SELECT * from
(SELECT column1, 'table1' table_name from table1
UNION ALL
SELECT column1, 'table2' table_name from table2
UNION ALL
... --repeat for all 10 tables
SELECT column1, 'table10' table_name from table10)
where column1 not like '123%' and length(column1) < 12;
这将 return 结果如下:
column1 table_name
abc table2
1245yy table5
这将为您提供每个 table 中第 1 列不符合长度和格式条件的所有记录。
编辑:
非常感谢UNION ALL
in place of UNION
is safe (and with performance boosts) because if the column1 happens to not have a unique index, then duplicate values coming from one table will be dropped. For some extra reading on the performance bonuses of using UNION ALL, check this link.