1

I have one table reports, which contains all info and read reports which just has Report ID and ID (of owner of the report)

I'm trying to do this statment (correct me if theres better out there) so it gets all the reports ID from read reports which match ID 1, and pick out all the details from reports for it. (Report ID's are the same on reports and read reports)

But this statement is giving me back no rows:

SELECT a.* 
 FROM `Reports` AS a, 
      (SELECT `Report ID` FROM `Read Reports` WHERE `Id` = 1) AS b
      WHERE a.`Report ID` = b.`Report ID`;

Whats wrong with it/how can I improve it?

Thanks,

EDIT: My bad, it works fine!! Id 1 had no reports. Close this. :L

EDIT2: Still post if you have improvements though :P

1
  • EDIT: My bad, it works fine!! Id 1 had no reports. Close this. :L Commented Feb 28, 2012 at 16:24

2 Answers 2

2

Try this:

SELECT a.*, (SELECT `Report ID` FROM `Read Reports` WHERE `Id` = 1) AS b_report_id
FROM `Reports` AS a
HAVING a.`Report ID` = b_report_id;
Sign up to request clarification or add additional context in comments.

3 Comments

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE a.Report ID = b_report_id
Try it now. I didn't remove the last comma after "From Reports AS a".
Still no :( #1054 - Unknown column 'b_report_id' in 'where clause'
1

There seems to be nothing wrong with your query and it should return the records unless there are no matching records. But if you say that there do exist matching records, I'd suggest that you re-read your query to confirm that you are using the right column names, i.e. not replaced "Id" with "Report ID"?

Can you give a snapshot of your data in your post?

By the way, the below query should be better because it does not involve derived table:

SELECT `a`.*
FROM `Reports` AS `a`
INNER JOIN `Read Reports` AS `b` ON `a`.`Report ID` = `b`.`Report ID`
WHERE `b`.`Id` = 1;

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.