MySQL Help
 
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 ForumsDatabasesMySQL Help

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:29 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:32 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by notflip
pretty confused here!
me too

are you sure you're not mixing up two separate processes?

1. retrieve data in simplest and most efficient manner

2. use application language to reformat query results
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old November 6th, 2012, 05:36 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 r937
me too

are you sure you're not mixing up two separate processes?

1. retrieve data in simplest and most efficient manner

2. use application language to reformat query results


How do i know what the most efficient manner is ?

Reply With Quote
  #4  
Old November 6th, 2012, 06:23 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by notflip
How do i know what the most efficient manner is ?
simple queries, no cosmetic restructuring, properly optimized with indexes, table joins as appropriate

Reply With Quote
  #5  
Old November 6th, 2012, 06:26 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 r937
simple queries, no cosmetic restructuring, properly optimized with indexes, table joins as appropriate


I know what you mean. But im very new to MySql. so right now it's finding your way and being happy when it' works I guess.. In the end I also want perfection and good db-performance

Reply With Quote
  #6  
Old November 6th, 2012, 06:37 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
I'm nearly there! Do you have ANY clue on how to solve this one... Subqueries freak me out for now

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
  #7  
Old November 6th, 2012, 07:10 PM
regany regany is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 h 53 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by notflip
The line in bold has to be higher then 0 to be counted


To only return those results which have average > 0 try something like this at the end...

Code:
HAVING average > 0


FYI - A HAVING clause acts like a filter on the results returned from the query above it. So the entire result is essentially created and then passed to your HAVING clause which filters out those rows which having average less than 0.

Reply With Quote
  #8  
Old November 6th, 2012, 07:38 PM
regany regany is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 h 53 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by notflip
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!


A UNION can join results from 2 individual queries, as long as each query returns the same column names and column count.

eg. (untested) ...

Code:
SELECT firstname, lastname FROM table1
UNION
SELECT firstname, lastname FROM table2
UNION
SELECT t3.fn as firstname, t3.ln as lastname FROM table3 as t3


As an example, this wouldn't work because the result set from each return a different number of columns:

Code:
SELECT firstname, middlename, lastname FROM table1
UNION
SELECT firstname, lastname FROM table2
Comments on this post
r937 agrees: thanks for taking the time to give examples! very helpful for new developers

Reply With Quote
  #9  
Old November 6th, 2012, 08:01 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by regany
... as long as each query returns the same column names and column count.
this is correct, except for the part about the same column names

the columns of all subselects in a union query must have

1. same number of columns
2. same datatype, vertically

the column names for the result table produced by a union query are taken only from the first subselect

for example...

SELECT a,b,c FROM table1
UNION
SELECT foo,bar,fap FROM jabberwocky
UNION
SELECT 1,2,3

the result set will have column names a,b,c

Reply With Quote
  #10  
Old November 7th, 2012, 03:42 AM
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
When i do it with the having clause at the end, The output is wrong.. It should check for the Condition inside the Subquery in the beginning. is this possible?

Reply With Quote
  #11  
Old November 7th, 2012, 03:57 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
>the columns of all subselects in a union query must have

>1. same number of columns
>2. same datatype, vertically

MySQL will forgive a rule 2 infringement - although I'm not sure why you'd want to mix columns of differing data types in a resultset!

Reply With Quote
  #12  
Old November 7th, 2012, 04:24 AM
regany regany is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level)regany User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 h 53 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by notflip
When i do it with the having clause at the end, The output is wrong.. It should check for the Condition inside the Subquery in the beginning. is this possible?


Not really sure what you mean by the output is wrong... you could throw a MAX in there so that clause will either return 0 or anything greater than 0 for the average, if that's what you're trying to do...

Code:
MAX((ROUND(AVG(antwoord_naam),1) * 10),0) as average

Reply With Quote
  #13  
Old November 7th, 2012, 04:47 AM
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
hmh. Almost there! Damn this is so complicated! I know what the problem is..

The part in bold. Has to exclude every 0 value in the average. So when it's 0, it can't be added to the average

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 = '$werknemerID'
GROUP BY categorieen.categorie_id");

Reply With Quote
  #14  
Old November 7th, 2012, 04:49 AM
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 notflip
hmh. Almost there! Damn this is so complicated! I know what the problem is..

The part in bold. Has to exclude every 0 value in the average. So when it's 0, it can't be added to the average

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 = '$werknemerID'
GROUP BY categorieen.categorie_id");


Code:
SELECT 
     SUM(case when antwoord_naam = 0 then 1 else 0 end) as niet_ingevuld,
     COUNT(antwoorden.vraag_id) as aantal,
     ( SELECT AVG(NULLIF(antwoord_naam ,0)) * 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");


This is the solution! Thank you very much! Didn't know the function!

Last edited by notflip : November 7th, 2012 at 05:17 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > 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