Jump to content

Problems with subquerys


devnill

Recommended Posts

I am having an issue in PHP where I keep getting errors when i try to run multiple queries using a single mysql_query(). I don't know if this is possible, but at this point I am assuming it is not because single queries run fine.

 

For what I am making, I need to either run 2 simultaneous queries, or use a subquery, but when I do that, the results are totally different. My query should be returning one row, but when the subquery runs, it never returns any rows at all? I am pretty sure this has to do with the order that the queries run, but I have no idea how to fix this. Any ideas?

 

Below is the before and after of my SQL when I use subqueries and 2 separate one:

 

 

Two seperate queries:

SELECT

CEILING(RAND()*COUNT(libNum))

INTO @randLib

FROM finishedLibs;

SELECT finishedLibs.finLib, madlibTemplates.madlib

FROM finishedLibs, madlibTemplates

WHERE madlibTemplates.mid = finishedLibs.mid

AND finishedLibs.libNum=@randLib

 

 

Single query with a subquery:

SELECT finishedLibs.finLib, madlibTemplates.madlib

FROM finishedLibs, madlibTemplates

WHERE madlibTemplates.mid = finishedLibs.mid

AND finishedLibs.libNum=(SELECT CEILING(RAND()*COUNT(libNum)) FROM finishedLibs)

 

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.