Advertisement

08.27.2008 at 08:18AM PDT, ID: 23682344
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.6

LEFT JOIN in MySQL with multiple ON expressions

Asked by pontupo in MySQL Server, MS SQL Server

Tags:

I'm quite well-versed in SQL Server 2000 syntax and Transact SQL but have recently had call to do some work connecting one of our SQL Server installations to a MySQL database. I'll give the whole picture because I'm not completely sure where the problem is here.

I connect to the MySQL database via Linked Servers and use OPENQUERY to execute MySQL queries on that machine from SQL Query Analyzer as I develop a new stored procedure that will utilize data from both database. I've run into a particular difference between MySQL and SQL Server that really has me stumped and none of the online documentation that I've found for MySQL indicates that what I'm trying to do is problematic. Essentially, consider the attached queries. In SQL Server, 2) would work just fine and it's exactly what I want (all rows from student_completed_section and only those rows from the subquery that join with those rows). In MySQL, however, 2) causes the database to hang (indefinitely, or at any rate many, many times longer than it should, such that I get fed up and kill the process), so I'm forced to approach the problem using 1). If I write a simple LEFT JOIN in MySQL with a single ON expression like:

SELECT *
FROM table1 LEFT JOIN table2 ON table1.table_id = table2.table_id

everything works fine, but combining multiple expressions in the ON statement seems to invariably lead to a massive decrease in query efficiency. What am I doing wrong here? The queries seem to have similar problems when executing them from the MySQL Query Manager as well, so I'm fairly certain it isn't caused by Linked Servers and OPENQUERY mechanisms and seems to be a confusion about syntax and the subtleties of joins in MySQL vs. SQL Server.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
--1)
SELECT comp.school_id, comp.class_id, comp.curriculum_id, comp.person_id, 
	comp.unit_id, MAX(comp.section_id) AS section_id
FROM student_completed_section comp,
	(SELECT MAX(unit_id) AS unit_id, school_id, class_id, curriculum_id, person_id
			FROM student_completed_section
			GROUP BY school_id, class_id, curriculum_id, person_id) sub
WHERE sub.school_id = comp.school_id
	AND sub.class_id = comp.class_id
	AND sub.curriculum_id = comp.curriculum_id
	AND sub.person_id = comp.person_id
	AND sub.unit_id = comp.unit_id
GROUP BY comp.school_id, comp.class_id, comp.curriculum_id, comp.person_id
 
--2)
SELECT comp.school_id, comp.class_id, comp.curriculum_id, comp.person_id, 
	comp.unit_id, MAX(comp.section_id) AS section_id
FROM student_completed_section comp
	LEFT JOIN (SELECT MAX(unit_id) AS unit_id, school_id, class_id, curriculum_id, person_id
			FROM student_completed_section
			GROUP BY school_id, class_id, curriculum_id, person_id) sub
	ON (sub.school_id = comp.school_id
		AND sub.class_id = comp.class_id
		AND sub.curriculum_id = comp.curriculum_id
		AND sub.person_id = comp.person_id
		AND sub.unit_id = comp.unit_id)
GROUP BY comp.school_id, comp.class_id, comp.curriculum_id, comp.person_id
 
Loading Advertisement...
 
[+][-]08.27.2008 at 09:01AM PDT, ID: 22326329

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.27.2008 at 09:23AM PDT, ID: 22326589

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.27.2008 at 09:32AM PDT, ID: 22326690

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.27.2008 at 09:40AM PDT, ID: 22326763

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.27.2008 at 09:51AM PDT, ID: 22326863

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.27.2008 at 10:12AM PDT, ID: 22327040

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.27.2008 at 11:16AM PDT, ID: 22327657

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.27.2008 at 11:40AM PDT, ID: 22327872

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.23.2008 at 01:35PM PDT, ID: 22553774

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]09.28.2008 at 06:49AM PDT, ID: 22590925

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MySQL Server, MS SQL Server
Tags: MySQL, Transact-SQL
Sign Up Now!
Solution Provided By: Computer101
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628