Jump to content

[SOLVED] Subquery


The Little Guy

Recommended Posts

How can I get this subquery to work?

 

SELECT * FROM users, videos WHERE videos.id = '%s'
AND
videos.owner = users.user AND (SELECT COUNT(id) as totalVids FROM videos) LIMIT 1

 

I don't get an error, but when I do a print_r(), it returns everything, except for what is in the subquery. How can I get this to work?

Link to comment
Share on other sites

Ok, just evaluate that last part in your head for a minute "...AND (SELECT COUNT(id) as totalVids FROM videos)..."

I have just one question when that subquery returns the COUNT(), i.e. number of records from the videos table, and you AND it onto your current statement what do you think it will look like. e.g. substitute a number in there to represent a potential result:

... AND 3 ...

 

Define that logic... " I want blah, blah, blah from some table where blah = something AND 3"

Link to comment
Share on other sites

Define "work".

 

By "work", I mean that the query returns all the results for the first select statement but not the second one.

 

so... the non-working version would look something like this (Some results removed due to length):

 

(user/password::table=users, title/tags/id::table=videos)

user                 password                 title                 tags                 id 
ryan                 8934j4ididd9          good movie            movie awesome       19b3ae5975ac

 

That is the results it returns, but it didn't return the subquery in the results, which would be the number: 0+

 

I read about subqueries at MySQL.com, but it didn't help me.

Link to comment
Share on other sites

By "work", I mean that the query returns all the results for the first select statement but not the second one.

...

That is the results it returns, but it didn't return the subquery in the results, which would be the number: 0+

Well, ahem, you didn't put the subquery in your SELECT column list, so how could it?

 

You'll have to write a dependent subquery that get evaluated for each id.

Link to comment
Share on other sites

Right, so explain what you want in plain english then.

Something like:

 

"I want the number of videos for EVERY user",

or

"I want ALL the video titles that Ryan likes".

 

What is the joining clause between the 2 tables? As in, how do you relate/map users to videos? You need an interim table by the looks of it. Unless of course i'm mistaken and the id field in the videos as being the id of the user? Is that the case?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.