SaranacLake Posted February 22, 2020 Share Posted February 22, 2020 (edited) I think I need a Subquery to solve my latest issue, but am unsure. (Boy am i rusty on SQL!!) Simplified example... Let's say that I have query #1 that joins a bunch of tables (too complicated to post here for now) and it's end result is the following... Quote +---------+------+---------------+ | Plan | Ver | Category | +---------+------+---------------+ | Basic | v2 | News | | Basic | v2 | Articles | | Basic | v2 | Op-Ed | +---------+------+---------------+ Now for query #2 - the one I need help with - I am trying to do something like this... SELECT * FROM article AS a WHERE (a.slug = $urlSlug) AND {a.category_id is equal to one of the Category values returned in Query #1) Edited February 22, 2020 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/310099-do-i-need-a-subquery/ Share on other sites More sharing options...
requinix Posted February 22, 2020 Share Posted February 22, 2020 You might not need a subquery. Depends what that "too complicated" query was. So just let us see it. Quote Link to comment https://forums.phpfreaks.com/topic/310099-do-i-need-a-subquery/#findComment-1574754 Share on other sites More sharing options...
SaranacLake Posted February 22, 2020 Author Share Posted February 22, 2020 1 minute ago, requinix said: You might not need a subquery. Depends what that "too complicated" query was. So just let us see it. It involves joining 3-4 tables - some with composites keys. I haven't written it yet. Either way, query #1 will return a result set that might be anywhere from 1 to 15 "Category" values. (Since the results will vary, that I why I was thinking query #1 would be in the WHERE part of query #2. Like... WHERE a.category_id IN (query #1 results) Quote Link to comment https://forums.phpfreaks.com/topic/310099-do-i-need-a-subquery/#findComment-1574756 Share on other sites More sharing options...
requinix Posted February 22, 2020 Share Posted February 22, 2020 Okay. How about you write it, then come back and say what it is? Because like I said, depending on what it is, you may not need the subquery. Quote Link to comment https://forums.phpfreaks.com/topic/310099-do-i-need-a-subquery/#findComment-1574759 Share on other sites More sharing options...
SaranacLake Posted February 23, 2020 Author Share Posted February 23, 2020 19 hours ago, requinix said: Okay. How about you write it, then come back and say what it is? Because like I said, depending on what it is, you may not need the subquery. While I am doing that, care to give me a hint what the alternative might be? 🙂 Quote Link to comment https://forums.phpfreaks.com/topic/310099-do-i-need-a-subquery/#findComment-1574794 Share on other sites More sharing options...
kicken Posted February 23, 2020 Share Posted February 23, 2020 18 hours ago, SaranacLake said: care to give me a hint what the alternative might be? Maybe just adding another join to your "complicated" query that connects the articles table. Quote Link to comment https://forums.phpfreaks.com/topic/310099-do-i-need-a-subquery/#findComment-1574826 Share on other sites More sharing options...
SaranacLake Posted February 23, 2020 Author Share Posted February 23, 2020 3 minutes ago, kicken said: Maybe just adding another join to your "complicated" query that connects the articles table. A-ha. Yeah, I was thinking that is what you'd say. No, I considered that, but in my ERD that wouldn't work because I have a fork like this... >|-------||- MY_TABLE -||-------< (I am brushing up on MySQL so I can create a "test" database and try out my hypotheses!) Quote Link to comment https://forums.phpfreaks.com/topic/310099-do-i-need-a-subquery/#findComment-1574828 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.