下面是2表。表格1:
id name
1 XYZ
2 ABC
3 POP
Table2:
id dName
1 xyz_1
2 abc_1
3 pop_1
4 kkk_1
Need to fetch IDs for Table1.name = 'XYZ',
Need to fetch IDs for Table2.dName = 'xyz_1',
Need to fetch IDs for Table2.dName = 'abc_1'
this IDs will be inserted to Table3:
insert into Table3 (s_id,p_id,d_id)
select Table1.id as s_id, Table2.id as p_id, Table2.id as d_id from Table1,Table2 where
Table1.name = 'XYZ'
Table2.dName = 'xyz_1'
Table2.dName = 'abc_1'
基本上上面的選擇查詢應該給出如下結果。這應該是我期望的上述查詢的結果。
s_id p_id d_id
1 1 2
Need help to define the proper query how I can get ids and directly insert ? No relationship between Table1 and Table2. Basically looking for single query which will give the result like above which I mentioned.
Thanks..
uj5u.com熱心網友回復:
條件 2 和 3 是獨立的,因此您必須為這些條件使用獨立的表副本:
SELECT t1.id s_id, t2.id p_id, t3.id d_id
FROM table1 t1
CROSS JOIN table2 t2
CROSS JOIN table2 t3
WHERE t1.name = 'XYZ'
AND t2.dName = 'xyz_1'
AND t3.dName = 'abc_1'
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383428.html
標籤:mysql
