-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhere and
148 lines (113 loc) · 4.82 KB
/
where and
1
I have a table plasmid_table with key plasmid_id (not shown), which is associated an antibiotic table (also not shown) with primary key antibiotic_id by way of the table below named plasmid_to_antibiotic_table.```plasmid_fk antibiotic_fk14 115 116 117 118 119 120 126 426 5```I want to find the sole record that contains both antibiotics 4 and 5, so I did a search as follows```SELECT plasmidName_col FROM plasmid_table INNER JOIN plasmid_to_antibiotic_table ON plasmid_table.plasmid_id = plasmid_to_antibiotic_table.plasmid_fk INNER JOIN antibiotic_table ON plasmid_to_antibiotic_table.antibiotic_fk = antibiotic_table.antibiotic_id WHERE (antibiotic_table.antibiotic_id = 4 AND antibiotic_table.antibiotic_id = 5)```This doesn't work, of course because antibiotic_table.antibiotic_id can't both be 4 and 5 in the same record.The solution I discovered is to to replace the WHERE clause withWHERE antibiotic_table.antibiotic_id IN (4,5)Group BY plasmidName_colhaving count(antibiotic_table.antibiotic_id) = 2However, it's going to get complicated because I need searches like these across multiple intermediate tables and even merely adding the second intermediate table like soINNER JOIN plasmid_to_antibiotic_table ON plasmid_table.plasmid_id = plasmid_to_antibiotic_table.plasmid_fk INNER JOIN antibiotic_table ON plasmid_to_antibiotic_table.antibiotic_fk = antibiotic_table.antibiotic_id INNER JOIN plasmid_to_fluoro_tag_table ON plasmid_table.plasmid_id = plasmid_to_fluoro_tag_table.plasmid_fk INNER JOIN fluoro_tag_table ON plasmid_to_fluoro_tag_table.fluoro_tag_fk = fluoro_tag_table.fluorotag_id causes the having clause to fail to return any results and I haven't even gotten to making the compound having clause yet for this second set of tables.plasmid_table = recipe tableantibiotic_table = ingredients tableplasmid_to_antibiotic_table = recipeingredients tableSELECT p.plasmid_id, p.plasmidName_colFROM plasmid_table pWHERE EXISTS( SELECT * FROM plasmid_to_antibiotic_table pa INNER JOIN antibiotic_table a ON a.antibiotic_id = pa.antibiotic_fk WHERE pa.plasmid_fk = p.plasmid_id AND a.antibiotic_id = 4)AND EXISTS( SELECT * FROM plasmid_to_antibiotic_table pa INNER JOIN antibiotic_table a ON a.antibiotic_id = pa.antibiotic_fk WHERE pa.plasmid_fk = p.plasmid_id AND a.antibiotic_id = 5)AND EXISTS ( SELECT * FROM plasmid_to_fluoro_tag_table pf INNER JOIN fluoro_tag_table f ON f.fluorotag_id = pf.fluoro_tag_fk WHERE pf.plasmid_fk = p.plasmid_id AND f.fluorotag_id = 4)AND EXISTS( SELECT * FROM plasmid_to_fluoro_tag_table pf INNER JOIN fluoro_tag_table f ON f.fluorotag_id = pf.fluoro_tag_fk WHERE pf.plasmid_fk = p.plasmid_id AND f.fluorotag_id = 5)SELECT plasmidName_col FROM plasmid_tableINNER JOIN plasmid_to_antibiotic_table ON plasmid_table.plasmid_id = plasmid_to_antibiotic_table.plasmid_fk INNER JOIN antibiotic_table ON plasmid_to_antibiotic_table.antibiotic_fk = antibiotic_table.antibiotic_id WHERE antibiotic_table.antibiotic_id IN (4,5)Group BY plasmidName_colhaving count(distinct antibiotic_table.antibiotic_id) = 2---------------------ÑSELECT DISTINCT plasmid_id,plasmid_table.plasmidName_col FROM plasmid_tableINNER JOIN plasmid_to_antibiotic_table ON plasmid_table.plasmid_id = plasmid_to_antibiotic_table.plasmid_fk INNER JOIN antibiotic_table ON plasmid_to_antibiotic_table.antibiotic_fk = antibiotic_table.antibiotic_id INNER JOIN plasmid_to_fluoro_tag_table ON plasmid_table.plasmid_id = plasmid_to_fluoro_tag_table.plasmid_fk INNER JOIN fluoro_tag_table ON plasmid_to_fluoro_tag_table.fluoro_tag_fk = fluoro_tag_table.fluorotag_id WHERE antibiotic_table.antibiotic_id IN (4,5)Group BY plasmidName_colhaving count(antibiotic_table.antibiotic_id) = 2Problem contributor maybe an outside contributor although it doesn't seem we rcord this for transgene. perhaps it shouldas a result, author field and contributor may not be the same.overview,research, shift 14 to the rightpublications,personnel,life in the lab,join usresourcesbody { margin-right: 14px; }185178 is landing page distancenext question how do we find out who the current author isit seems we get it from the $_SESSION['user'] variableif (isset($_SESSION['user']) { author_fk = $_SESSION['user'] editor_fk = $_SESSION['user'];}search will search them both, but how does it do that because right now we have LEFT JOIN contributor_table ON truestrain_table.authorID_col = contributor_table.contributor_id ';LEFT JOIN contributor_table ON truestrain_table.editorID_col = contributor_table.contributor_id ';turn these innner joins to left joins so we don't miss either onewe need two functions in search to search across these tablesnow how do we save the editor