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
https://forums.phpfreaks.com/topic/110667-problems-with-subquerys/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.