我有3张表A,B,C
表A
TABLE_NAME|table_number
soho 20
foho 30
joho 40
表B
TABLE_ID | TABLE_NAME
1 soho
2 foho
3 joho
表C
TABLE_ID | TABLE_VALUES
1 xx
1 yy
2 hh
3 no
3 bb
我想要做的是将table_name作为:paramter
从表c中获取table_values
,如果我通过soho
TABLE_NAME| TABLE_VALUES
SOHO xx
SOHO yy
这是我的尝试,但我得到了所有表格的table_value
select a.table_name , c.table_value
from a , b , c
where a.table_name= :myParamter
and
b.table_id= c.table_id
我的查询输出就像这样
ABLE_NAME| TABLE_VALUES
SOHO xx
SOHO yy
SOHO hh
SOHO no
SOHO bb
答案 0 :(得分:5)
select b.table_name , c.table_value
from b inner join c
on
b.table_id= c.table_id
where b.table_name= :myParamter
答案 1 :(得分:1)
表DDL
CREATE TABLE TABLEA
(
TABLE_NAME VARCHAR2(64),
TABLE_NUMBER NUMBER(18)
);
CREATE TABLE TABLEB
(
TABLE_ID NUMBER(18),
TABLE_NAME VARCHAR2(64)
);
CREATE TABLE TABLEC
(
TABLE_ID NUMBER(18),
TABLE_VALUES VARCHAR2(10)
);
数据插入
INSERT INTO TABLEA VALUES('soho',20);
INSERT INTO TABLEA VALUES('foho',30);
INSERT INTO TABLEA VALUES('joho',40);
INSERT INTO TABLEB VALUES(1,'soho');
INSERT INTO TABLEB VALUES(2, 'foho');
INSERT INTO TABLEB VALUES(3, 'joho');
INSERT INTO TABLEC VALUES(1, 'xx');
INSERT INTO TABLEC VALUES(1,'yy');
INSERT INTO TABLEC VALUES(2, 'hh');
INSERT INTO TABLEC VALUES(3, 'no');
INSERT INTO TABLEC VALUES(3, 'bb');
查询 -
select b.table_name, c.table_values
from tableb b inner join tablec c
on b.table_id = c.table_id
inner join tablea a
on a.table_name = b.table_name
and b.table_name = 'soho'
;
输出
TABLE_NAME,TABLE_VALUES
soho,yy
soho,xx