Jump to content

Do I need a Subquery?


Recommended Posts

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 by SaranacLake
Link to post
Share on other sites
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)
	

 

 

Link to post
Share on other sites
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?  🙂

 

Link to post
Share on other sites
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!)

 

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.