sql >> Database teknologi >  >> RDS >> Mysql

SQL:loop gennem samme tabel

sådan noget? FIDDLE til reference

SELECT project_id,project_name,parent_id,recycle_bin,creater_idFROM projectdetails AS pd1JOIN( SELECT parent_id FROM projectdetails as pd WHERE pd.parent_id> 0 AND pd.recycle_bin> 1) AS t ON t.parent_id =pd1.project_id 

med nye data.. NEW_FIDDLE

SELECT if(pd1.recycle_bin>1, pd1.project_id, t.project_id) AS project_id, if(pd1.recycle_bin>1, pd1.project_name, t.project_name) AS project_name, if(pd1.recycle_bin>1, pd1.parent_id, t.parent_id) AS parent_id, if(pd1.recycle_bin>1, pd1.recycle_bin, t.recycle_bin) AS recycle_bin, if(pd1.recycle_bin>1, pd1.creater_id, t.creater_id) AS creater_idFROM projectdetails AS pd1JOIN( SELECT * FROM projectdetails AS pd WHERE pd.parent_id> 0 AND pd.recycle_bin> 1) AS t ON t.parent_id =pd1.project_idGROUP BY project_id

uden at bruge IF's LAST_FIDDLE

SELECT pd1.project_id, pd1.project_name, pd1.parent_id, pd1.recycle_bin, pd1.creater_idFROM projectdetails AS pd1WHERE NOT EXISTS( SELECT pd.recycle_bin FROM projectdetails as pd WHERE pd1.parent_id =pd.project_id =pd.project_id .recycle_bin> 1) OG pd1.creater_id =7923 og pd1.recycle_bin> 1GROUP BY pd1.project_id; 


  1. Anvendelse af kolonnetilladelser for en tabel over en trigger

  2. Hvorfor vælger denne forespørgsel kun en enkelt række?

  3. PL/Python &postgreSQL:Hvad er den bedste måde at returnere en tabel med mange kolonner på?

  4. qt 5.8 sql forbindelsesfejl:QMYSQL driver ikke indlæst på Windows 10