xcandiottix Posted August 16, 2010 Share Posted August 16, 2010 I need to perform a search of my DB using 6 fields. I want to progress thru the searches as so: Row: A B C D E F Select A, if A then select B, if B then select C if C then select D, if D select E, if E then select F I then want to extract the data from each field. if A then $a = A, if B then $b = B, etc etc. I don't want to have a whole bunch of different select statements or if conditions if I don't need to. Also, the DB is rather large so i think evaluating: $query = "SELECT * FROM table" Might be overkill. Is there anyway to do something like: $queryA = SELECT * FROM table WHERE A = subject1 if($queryA){$queryB = SELECT * FROM $queryA WHERE B = subject2} if($queryB){$queryC = SELECT * FROM $queryB WHERE C = subject3} etc etc etc So basically I just progressively filter down one original mysql query to get what I need. Quote Link to comment Share on other sites More sharing options...
btherl Posted August 16, 2010 Share Posted August 16, 2010 You can select them all and then order them so that the result you want appears first .. the exact sql depends on your database structure. But it won't be pretty. I suggest you do it in php unless you have a good reason not to. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 16, 2010 Share Posted August 16, 2010 Maybe I am not understanding the request - kind of confusing. But, it seems he is just wanting a query with multiple where clauses: SELECT * FROM table WHERE A = subject1 AND B = subject2 AND C = subject3 AND D = subject4 AND E = subject5 AND F = subject6 Quote Link to comment Share on other sites More sharing options...
btherl Posted August 17, 2010 Share Posted August 17, 2010 Hmm, on re-reading the original post, this might be a job for left joins .. eg SELECT A,B,C,D,E,F FROM tab1 LEFT JOIN tab2 ON (tab2.A = tab1.A) LEFT JOIN tab3 ON (tab3.B = tab2.B) ... WHERE A = subject1 That implements "If there's a result from tab1, then fetch matching data from tab2. If there's a result from tab2, fetch matching data from tab3" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.