简体   繁体   English

MySQL UNION查询中的行名称相同

[英]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查询之前:

  • RDMBS tries to determine number of columns that the whole UNION-query will return (by looking on numbers of columns that specific SELECT-queries are expected to return). RDMBS尝试确定整个UNION查询将返回的列数(通过查看预期特定SELECT查询将返回的列数)。 If two SELECT-queries in the UNION-query are expected to return different number of columns — it's error. 如果期望UNION查询中的两个SELECT查询返回不同的列数-这是错误的。
  • RDMBS tries to determine types of columns that the whole UNION-query will return (by looking on types of columns that specific SELECT-queries are expected to return). RDMBS尝试确定整个UNION查询将返回的列的类型(通过查看期望特定SELECT查询返回的列的类型)。 If types of corresponding columns in two SELECT-queries in the UNION-query are too different (that RDBMS cannot cast them to common supertype) — it's error. 如果UNION查询中两个SELECT查询中对应列的类型过于不同 (RDBMS无法将它们强制转换为通用超类型)—这是错误的。
  • RDMBS tries to determine names of columns that the whole UNION-query will return (by looking on names of columns that specific SELECT-queries are expected to return). RDMBS尝试确定整个UNION查询将返回的列的名称(通过查看预期将返回特定SELECT查询的列的名称)。 If names of corresponding columns in two SELECT-queries in the UNION-query are different — it's not so important, RDBMS takes first one. 如果UNION查询中两个SELECT查询中对应列的名称不同-并不是那么重要,则RDBMS会采用第一个。 (Theoretically, it's not so important, and designed-in-another-way RDBMS would take the second one, or even combine them in some way — but the fact is that all collumn names must be known without executing the query.) (从理论上讲,它并不那么重要,而以另一种方式设计的RDBMS将采用第二种,甚至以某种方式将它们组合在一起-但事实是必须知道所有列名称,而不执行查询。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM