如何从同一个表但具有不同查询的数据中获取数据,但结果表是一个 [英] how to get data from same table but with different queries but the resultated table is one

查看:108
本文介绍了如何从同一个表但具有不同查询的数据中获取数据,但结果表是一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

就像我有一个表和两个查询,两个查询从两个表中获取不同的数据,但是两个字段的名称都相同,但是我希望两个查询生成的表将是一个,但字段不同.

例如这样的原始桌子

id名称age
1 Abc 12
2 aba 13
3 abb 13
4 bca 10
和查询是
1从table_name中选择*,其中age = 12
2从table_name中选择*,其中age = 13

我想要结果表是

id名称年龄id1 name1 age1
1 abc 12 2 aba 13
3 abb 13

我想要此

Hello guys

Just like I have one table and two queries and both queries get different data from both table but the field name of both are same but i want that the table generate by the two queries will be one but with ddifferent fields .

eg Orignal table like this

id name age
1 Abc 12
2 aba 13
3 abb 13
4 bca 10
and queries are
1 select * from table_name where age=12
2 select * from table_name where age=13

and I want resulted table is

id name age id1 name1 age1
1 abc 12 2 aba 13
3 abb 13

I want sql query for this

推荐答案

的sql查询,如果我正确理解了您的问题,则希望执行2个查询,这些查询返回具有相同字段的2个结果集.在您的情况下,您只需要像这样使用OR运算符或IN运算符
If I understood your question correctly, you want to perform 2 queries that return 2 result sets that have the same fields. In your case, all you need is using the OR operator or the IN operator like this
select * from table_name where age=12 or age=13
-- or
select * from table_name where age IN (12, 13)


或者您使用 UNION运算符 [


or you use the UNION operator[^] like this

select * from table_name where age=12
UNION
2 select * from table_name where age=13


UNION运算符用于连接具有相同结构(相同数量的列和相同顺序的相似数据类型)的2个结果集.但是在您的情况下,OR运算符或IN运算符就足够了.


The UNION operator is used to concatenate 2 result sets that have the same structure (same number of columns and similar data types in the same order). But in your case, the OR operator or the IN operator is enough.


这篇关于如何从同一个表但具有不同查询的数据中获取数据,但结果表是一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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