简体   繁体   English

一个查询到另一个查询的结果

[英]Result of one query into another query

I have two tables TABLE_A with columnname COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 and which has data 我有两个表TABLE_A,列名称为COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5,并且其中有数据

abc def ghi jkl mno abc def ghi jkl mno

123 456 789 001 121 123456789001121

TABLE_B with columnname COLUMN6 COLUMN7 which has data as 名称为COLUMN6 COLUMN7的TABLE_B ,其数据为

COLUMN5 124 栏5 124

COLUMN4 bca COLUMN4 bca

COLUMN3 aaa COLUMN3 aaa

COLUMN5 bbb COLUMN5 bbb

So I have the columnname of Table_A as a data in the table_B 所以我将Table_A的列名作为table_B中的数据

so I want to do something like this in a single query 所以我想在一个查询中做这样的事情

$query1= select COLUMN6 from TABLE_B where COLUMN7='aaa'; $ query1 =从TABLE_B中选择COLUMN6,其中COLUMN7 ='aaa';

$query2= select $query1 from TABLE_A where COLUMN1='123'; $ query2 =从TABLE_A中选择$ query1,其中COLUMN1 ='123';

Thanks 谢谢

You need to have a value to match from table a -> to table B If it looks like this: 您需要具有一个值以从表a->匹配到表B,如果看起来像这样:

TableA -> id, name TableA-> ID,名称
TableB -> id, table_a_id, name TableB-> id,table_a_id,名称

This query will work: 此查询将起作用:

SELECT a.name, b.name
FROM tableA as a
JOIN tableB as b ON a.id=b.table_a_id AND b.name='123'
WHERE a.name='aaaa'

To get the names of table A and B. I am using an alias for the table names here to make it easier to read. 为了获得表A和B的名称。我在这里使用别名作为表名,以使其更易于阅读。 I hope, with this example, this will answer your question. 我希望通过这个例子,可以回答您的问题。

If you don't have any matching values, but you want to have all columns crossed, you can do this: 如果您没有任何匹配的值,但是希望所有列都交叉,则可以执行以下操作:

SELECT a.name, b.name
FROM tableA a, tableB b
WHERE a.name='aaa' AND b.name='123'

You may use CASE or DECODE to do that: 您可以使用CASE或DECODE来做到这一点:

select a.* from tableA a, tableB b
WHERE
b.column7 = 'aaa'
and case
when b.column6 = 'COLUMN1' then a.column1
when b.column6 = 'COLUMN2' then a.column2
when b.column6 = 'COLUMN3' then a.column3
when b.column6 = 'COLUMN4' then a.column4
when b.column6 = 'COLUMN5' then a.column5
else null end = '123' -- condition for tableA

You can make this statement up to 1000 columns (ORACLE hard limit) :) 您最多可以使该语句达到1000列(ORACLE硬限制):)

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

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