[英]Same row names in MySQL UNION query
I'm building a simple php search engine that will search in multiple tables. 我正在构建一个简单的PHP搜索引擎,它将在多个表中进行搜索。 Some tables have the same column name so I started using "AS" to identify each one. 有些表具有相同的列名,因此我开始使用“ AS”来标识每个表。
Here's the query that I'm using: 这是我正在使用的查询:
$query = " (SELECT coluna1 as txt1_festival, coluna2 as txt2_festival, 'festival' as tabela FROM tb_festival WHERE coluna1 LIKE '%" . $buscar . "%' OR coluna2 LIKE '%" . $buscar ."%')
UNION
(SELECT pergunta, resposta, 'faqs' as tabela FROM tb_faqs WHERE pergunta LIKE '%" . $buscar . "%' OR resposta LIKE '%" . $buscar ."%')";
The problem is that I'm getting all results with the same column names txt1_festival and txt2_festival, even when the result comes from the second table which columns names are pergunta and resposta. 问题是,即使结果来自第二个表,列名称分别是pergunta和resposta,我却得到了具有相同列名txt1_festival和txt2_festival的所有结果。
Can anyone tell me what should I do to achieve it?! 谁能告诉我该怎么做?!
It's the consequence of using UNION operator — it names the columns by the names of the first table in query. 这是使用UNION运算符的结果-它通过查询中第一个表的名称来命名列。 Please see the documentation 请参阅说明文件
If I have understood correctly what you want... 如果我正确理解了您想要什么...
You may use something like this: 您可以使用以下方式:
$query = "
(SELECT coluna1 as txt1_festival, coluna2 as txt2_festival, null as pergunta, null as resposta, 'festival' as tabela FROM tb_festival WHERE coluna1 LIKE '%" . $buscar . "%' OR coluna2 LIKE '%" . $buscar ."%')
UNION
(SELECT null as txt1_festival, null as txt2_festival, pergunta, resposta, 'faqs' as tabela FROM tb_faqs WHERE pergunta LIKE '%" . $buscar . "%' OR resposta LIKE '%" . $buscar ."%')
";
But IMHO this is a weird way to do the things. 但是恕我直言,这是一种奇怪的方式。 If you have to search for something in several tables (which structure and number of columns may vary), better just run several queries. 如果您必须在多个表中搜索某些内容(结构和列数可能会有所不同),最好运行几个查询。
The fact is that "structure" (ie number of columns, their names and types) of query result must be determinable without executing the query. 事实是查询结果的“结构”(即列数,它们的名称和类型)必须是可确定的,而无需执行查询。
Ie suppose that you have table tbl1
with fields (id integer, creation_time timestamp, val1 text, val2 text)
and are going to execute query SELECT id, id+2 as id_plus_2, creation_time AS cr_time FROM tbl1
. 即假设您具有带有字段(id integer, creation_time timestamp, val1 text, val2 text)
表tbl1
,并将执行查询SELECT id, id+2 as id_plus_2, creation_time AS cr_time FROM tbl1
。 Without even executing the query, RDBMS will understand that resulting structure should be (id integer, id_plus_2 integer, cr_time timestamp)
. 甚至不执行查询,RDBMS就会知道结果结构应该是(id integer, id_plus_2 integer, cr_time timestamp)
。
Therefore you cannot do for example stuff like this: 因此,您无法执行以下操作:
SELECT id, val1 AS 〈I_wanna_name_this_column_as_qq_if_id_is_less_than_17_and_as_pp_otherwise〉, val2 FROM tbl1;
or: 要么:
SELECT id, val1, val2 AS 〈I_wanna_name_this_column_as_qq_if_today_is_tuesday_and_as_pp_otherwise〉 FROM tbl1;
The same with UNION-using queries. 与使用UNION的查询相同。 RDMBS must know structure of the UNION-query result just after reading the query (assuming it already knows structures of tables). RDMBS必须在读取查询后立即知道UNION查询结果的结构(假设它已经知道表的结构)。 Therefore, even before executing the UNION-query: 因此,即使在执行UNION查询之前:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.