PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 6th, 2012, 05:23 PM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
Display 2 queries in the same table?

I have 2 Queries. but i want to show them in the same table. is it possible to iterate through them at the same time? pretty confused here!

--------------------------------------------------- |
category_name (query1) | average score (query1) |
-----------------------------------------------------
number of empty_questions (query2) |
-----------------------------------------------------

--------------------------------------------------- |
category_name (query1) | average score (query1) |
-----------------------------------------------------
number of empty_questions (query2) |
-----------------------------------------------------

Thanks!

Reply With Quote
  #2  
Old November 6th, 2012, 05:33 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,711 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 6 h 45 m 48 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
What are the queries you're running?

Reply With Quote
  #3  
Old November 6th, 2012, 05:35 PM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
Quote:
Originally Posted by requinix
What are the queries you're running?


They're pretty complicated.. to me..

Code:
$gemiddeldePerCategorie = mysql_query("
	SELECT ROUND(AVG(antwoord_naam),1) * 10 as average, categorieen.categorie_naam
	FROM antwoorden
	INNER JOIN vragen ON antwoorden.vraag_id = vragen.vraag_id
	INNER JOIN categorieen ON categorieen.categorie_id = vragen.categorie_id
	WHERE antwoorden.werknemer_id = '$werknemerID'
	GROUP BY categorieen.categorie_id");


Code:
$nietIngevuld = mysql_query("
	SELECT COUNT(antwoorden.vraag_id) as niet_ingevuld, categorieen.categorie_naam
	FROM antwoorden
	INNER JOIN vragen ON antwoorden.vraag_id = vragen.vraag_id
	INNER JOIN categorieen ON categorieen.categorie_id = vragen.categorie_id
	WHERE antwoorden.werknemer_id = '$werknemerID' AND antwoord_naam = 0
	GROUP BY categorieen.categorie_id
");

Reply With Quote
  #4  
Old November 6th, 2012, 05:44 PM
schworak schworak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 11 schworak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 11 sec
Reputation Power: 0
It really looks like you can combine those two queries into a single query to get multiple columns back. That would be the best way to keep your data synchronized to each other.

If you really need two queries, get a key from each of them that could be used to link them together row by row, then perform a larger query with each smaller query as a subquery in the from and again join them together to get a single dataset back.

Otherwise, you would need some sort of key and compare step by step through the data moving forward on one query or the other (or both) and that is rather painful.

So if possible, use a JOIN to get your data into a single recordset.

Reply With Quote
  #5  
Old November 6th, 2012, 05:48 PM
schworak schworak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 11 schworak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 43 m 11 sec
Reputation Power: 0
Code:
SELECT 
     sum(case when antwoord_naam = 0 then 1 else 0 end) as niet_ingevuld,
     ROUND(AVG(antwoord_naam),1) * 10 as average, 
     categorieen.categorie_naam
FROM antwoorden
INNER JOIN vragen ON antwoorden.vraag_id = vragen.vraag_id
INNER JOIN categorieen ON categorieen.categorie_id = vragen.categorie_id
WHERE antwoorden.werknemer_id = '$werknemerID'
GROUP BY categorieen.categorie_id"

Reply With Quote
  #6  
Old November 6th, 2012, 05:50 PM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
Quote:
Originally Posted by schworak
Code:
SELECT 
     sum(case when antwoord_naam = 0 then 1 else 0 end) as niet_ingevuld,
     ROUND(AVG(antwoord_naam),1) * 10 as average, 
     categorieen.categorie_naam
FROM antwoorden
INNER JOIN vragen ON antwoorden.vraag_id = vragen.vraag_id
INNER JOIN categorieen ON categorieen.categorie_id = vragen.categorie_id
WHERE antwoorden.werknemer_id = '$werknemerID'
GROUP BY categorieen.categorie_id"


Nice! Never heard of the CASE function. I'll look into that! didn't know it was ever possible to get this Query into 1! Thank you so much!

Reply With Quote
  #7  
Old November 6th, 2012, 06:20 PM
notflip's Avatar
notflip notflip is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 148 notflip User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 39 m 9 sec
Reputation Power: 1
There's one more problem I'm trying to solve. But i can't seem to get a WHERE clause inside this one? The line in bold has to be higher then 0 to be counted but

WHERE antwoord_naam > 0 can't be inserted because it's already a subquery?

Code:
SELECT 
     sum(case when antwoord_naam = 0 then 1 else 0 end) as niet_ingevuld,
COUNT(antwoorden.vraag_id) as aantal,
     ROUND(AVG(antwoord_naam),1) * 10 as average, 
     categorieen.categorie_naam
FROM antwoorden
INNER JOIN vragen ON antwoorden.vraag_id = vragen.vraag_id
INNER JOIN categorieen ON categorieen.categorie_id = vragen.categorie_id
WHERE antwoorden.werknemer_id = 14
GROUP BY categorieen.categorie_id

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Display 2 queries in the same table?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap