0

Here is my sql data fiddler http://sqlfiddle.com/#!2/63178/1. Waht's wrong with my query?

SELECT DISTINCT curr.id,curr.curr_tittle, curr.curr_desc 
FROM  wp_curriculum curr LEFT JOIN (SELECT DISTINCT * FROM wp_curriculum_topic WHERE curr_topic IN (4,12)) AS A ON A.curr_id = curr.id ORDER BY A.id
7
  • 3
    What are you trying to do? Commented Sep 18, 2013 at 6:42
  • 2
    what result you expecting? or you want? Commented Sep 18, 2013 at 6:43
  • what result set you expect and what is going wrong..pls explain? Commented Sep 18, 2013 at 6:46
  • can i join these 2 tables : SELECT DISTINCT id,curr_tittle, curr_desc FROM wp_curriculum WHERE id IN (SELECT DISTINCT curr_id FROM wp_curriculum_category WHERE curr_category IN (2,3)) SELECT DISTINCT id,curr_tittle, curr_desc FROM wp_curriculum WHERE id IN (SELECT DISTINCT curr_id FROM wp_curriculum_topic WHERE curr_topic IN (4,12)) Commented Sep 18, 2013 at 6:52
  • 1
    We don't know the structure of your table wp_curriculum_category... Also I think curr_id should be a foreign key. Commented Sep 18, 2013 at 6:59

2 Answers 2

1

If you are looking for matching row from both the table then just replace LEFT JOIN to INNER JOIN, otherwise your sql query is showing expected result for LEFT JOIN condition.

SQL Query with INNER JOIN:

SELECT DISTINCT curr.id,curr.curr_tittle, curr.curr_desc FROM  wp_curriculum curr INNER JOIN (SELECT DISTINCT * FROM wp_curriculum_topic WHERE curr_topic IN (4,12)) AS A ON curr.id = A.curr_id ORDER BY A.id
Sign up to request clarification or add additional context in comments.

2 Comments

you can actually do it without using subquery.
SELECT DISTINCT curr.id,curr.curr_tittle, curr.curr_desc FROM wp_curriculum curr INNER JOIN wp_curriculum_topicAS A ON curr.id = A.curr_id WHERE a.curr_topic IN (4,12) ORDER BY A.id
0

Your query works as expected. Could it be you are mixing ID and CURR_ID?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.