如何获取nname为" new5"的表名。在所有表中?。我已经尝试了下面的查询,但我没有得到解决方案(我不认为查询是对的)。我真的很感激任何帮助。谢谢你提前。
http://sqlfiddle.com/#!2/aa1b8/7
CREATE TABLE if not exists tblC1
(
id int(11) NOT NULL auto_increment ,
nname varchar(255),
PRIMARY KEY (id)
);
INSERT INTO tblC1 (id, nname) VALUES
('1', 'new1'),
('2', 'new2'),
('3', 'new3'),
('4', 'new4'),
('5', 'new5');
CREATE TABLE if not exists tblC2
(
id int(11) NOT NULL auto_increment ,
nname varchar(255),
PRIMARY KEY (id)
);
INSERT INTO tblC2 (id, nname) VALUES
('1', 'new1'),
('2', 'new21'),
('3', 'new31'),
('4', 'new41'),
('5', 'new51');
CREATE TABLE if not exists tblC3
(
id int(11) NOT NULL auto_increment ,
nname varchar(255),
PRIMARY KEY (id)
);
INSERT INTO tblC3 (id, nname) VALUES
('1', 'new1'),
('2', 'new21'),
('3', 'new31'),
('4', 'new41'),
('5', 'new5');
查询:
SELECT *
FROM (SELECT * FROM tblC1
UNION SELECT * FROM tblC2
UNION SELECT * FROM tblC3 ) as t2
where nname = "new5"
答案 0 :(得分:1)
将表名添加到您的单个选择中:
SELECT * FROM (
SELECT
'tblC1' as tbl, tblC1.*
FROM tblC1
UNION SELECT 'tblC2', tblC2.* FROM tblC2
UNION SELECT 'tblC3', tblC3.* FROM tblC3
) t
where t.nname = "new5"
应该这样做。对于每个联合选择,您需要外部选择或where子句。而且你必须指定表名,