If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > How to narrow resultset?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-20-09, 10:43
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
How to narrow resultset?

Hi,

Code:
SELECT     Student.surname, Courses.name, Phone.number, Email.address
FROM         Academics INNER JOIN
                      Student ON Academics.studentID = Student.studentID INNER JOIN
                      Courses ON Academics.courseID = Courses.courseID INNER JOIN
                      Email ON Student.studentID = Email.studentID INNER JOIN
                      Phone ON Student.studentID = Phone.studentID
WHERE     (Academics.academicYear = 2) AND (Student.surname LIKE N'James')
surname courseName number email
james BPA 7262650 zai@hotmail.com
james BPA 7262650 zai@yahoo.com

how do i go about this so that I don't get repeated data in my resultset? Thanks.
Reply With Quote
  #2 (permalink)  
Old 08-20-09, 11:04
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT Student.surname
     , Courses.name
     , Phone.number
     , GROUP_CONCAT(Email.address) AS email_addresses
  FROM Student
INNER 
  JOIN Academics  
    ON Academics.studentID = Student.studentID
   AND Academics.academicYear = 2
INNER 
  JOIN Courses 
    ON Courses.courseID = Academics.courseID
INNER 
  JOIN Email 
    ON Email.studentID = Student.studentID
INNER 
  JOIN Phone 
    ON Phone.studentID = Student.studentID
 WHERE Student.surname = 'James'
GROUP
    BY Student.surname
     , Courses.name
     , Phone.number  
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-20-09, 13:54
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
"GROUP_CONCAT is not a recognised built-in function name" in Visual Web Developer 2005
Reply With Quote
  #4 (permalink)  
Old 08-20-09, 14:09
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
You're right, its a MySQL function
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 08-20-09, 15:03
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
so i cant execute it on VWD? Also how will the resultset be following that query?
Reply With Quote
  #6 (permalink)  
Old 08-20-09, 15:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by kpeeroo
Also how will the resultset be following that query?
you can see this for yourself by running the query right in mysql

use the mysql query browser, or phpmyadmin, or sqlyog, or heidisql, or navicat, or ...

... or if you don't have any of those, use the mysql command line
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 12-15-09, 16:45
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
Any help on this using Sql Server syntax? Thanks.
Reply With Quote
  #8 (permalink)  
Old 12-15-09, 16:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by kpeeroo View Post
Any help on this using Sql Server syntax? Thanks.
why would you want SQL Server syntax if you're running against a MySQL database?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 12-15-09, 17:11
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
Actually I am using SQL Server 2005 and googling this topic I found no easy way to replace the GROUP_CONCAT() function. Most of the results are saying you have to create your own UDF for this except for one result using the 'for xml path('')' or something but I dont know how to incorporate this in the SQL statement
Reply With Quote
  #10 (permalink)  
Old 12-15-09, 18:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 12-16-09, 09:48
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
ok so that means i need to create a function and then using your above sql statement, pass on the email values to that function instead oF GROUP_CONCAT?
Reply With Quote
  #12 (permalink)  
Old 12-16-09, 10:18
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
If you don't care about the email address (only want one) then simply add group by Student.surname, Courses.name, Phone.number to your original query.
Reply With Quote
  #13 (permalink)  
Old 12-16-09, 13:04
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
Thanks for the reply but I think I would like all the email addresses like the resultset set shown, ie all email addresses with other columns so that I don't have to create another query. A single query grouping eveything would be nice if that can be done. So it seems that this UDF would do the job. Will try it and post back. I guess the email addresses would be passed to the UDF as a parameter and the resultset returned like in the article mentioned above. Correct?
Code:
SELECT Student.surname
     , Courses.name
     , Phone.number
     , MY_FUNCTION(Email.address) AS email_addresses
  FROM Student
INNER 
  JOIN Academics  
    ON Academics.studentID = Student.studentID
   AND Academics.academicYear = 2
INNER 
  JOIN Courses 
    ON Courses.courseID = Academics.courseID
INNER 
  JOIN Email 
    ON Email.studentID = Student.studentID
INNER 
  JOIN Phone 
    ON Phone.studentID = Student.studentID
 WHERE Student.surname = 'James'
GROUP
    BY Student.surname
     , Courses.name
     , Phone.number
Reply With Quote
  #14 (permalink)  
Old 12-16-09, 15:22
kpeeroo kpeeroo is offline
Registered User
 
Join Date: Jul 2009
Posts: 143
Don't know how to make this work. Any help on this? What if Crystal Reports is used for generating reports so these TSQL statements will be generated automatically by the IDE right?

Last edited by kpeeroo; 12-16-09 at 15:34.
Reply With Quote
  #15 (permalink)  
Old 12-16-09, 17:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by kpeeroo View Post
Don't know how to make this work.
replace MY_FUNCTION with MAX

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On