如何在4个不同的表格中搜索数据 [英] how to search data in 4 different tables

查看:145
本文介绍了如何在4个不同的表格中搜索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在不同的表中搜索数据,但是如果可能的话,只用一个查询;

I would like to know on how to search for a data in different table but with only one query if it is possible, example;

select * from table1,table2,table3 WHERE id="$searctext";



此查询是否适用或有更好的查询?

供您参考,这4个表具有相同的属性,例如table1的ID名称为address.

表2,3,4也是这样,但彼此之间没有关系,因此我要按id搜索,例如,我将其输入为001,而查询的作用是搜索所有4个表以获得id和有关ID的所有信息.

请帮我解决这个问题,我们将不胜感激:)



is this query applicable or there''s any better query?

for your information, those 4 tables have all the same attributes, for example, table1 has id name address.

so does the table 2,3,4 but did not relate to one another, so im going to search for example by id, I input it as 001, and what the query do is it search through all 4 tables to obtain the id and all information about the id.

Please help me with this, all help are much appreciated :)

推荐答案

searctext";
searctext";



此查询是否适用或有更好的查询?

供您参考,这4个表具有相同的属性,例如table1的ID名称为address.

表2,3,4也是这样,但彼此之间没有关系,因此我要按id搜索,例如,我将其输入为001,而查询的作用是搜索所有4个表以获得id和有关ID的所有信息.

请帮我解决这个问题,我们将不胜感激:)



is this query applicable or there''s any better query?

for your information, those 4 tables have all the same attributes, for example, table1 has id name address.

so does the table 2,3,4 but did not relate to one another, so im going to search for example by id, I input it as 001, and what the query do is it search through all 4 tables to obtain the id and all information about the id.

Please help me with this, all help are much appreciated :)


您可以使用以下查询,前提是各列完全匹配:
You can use the following query provided the columns match exactly :
select * from table1 where id = 'somevalue' 
union 
select * from table2 where id = 'somevalue'
union
...


我同意Mehdi的回答,但是如果您需要知道每行来自哪个表,我将添加此内容:

I agree with Mehdi''s answer, but I''ll add this in case you need to know which table each row is coming from:

SELECT *, ''table1'' AS tbl FROM table1 WHERE id=''


这篇关于如何在4个不同的表格中搜索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆