[Solved] SQL request with JOIN and COUNT

If I am reading your question right, the result table give you the age ID and Period, and then the count of toys per age ID and Period. Here is how you would write that query: SELECT Ages.ID, Ages.Period, IFNULL(sub.cnt,0) AS Count FROM Ages LEFT JOIN (SELECT Toys.age_id, COUNT(*) AS cnt FROM Toys GROUP BY … Read more

[Solved] joins in MySQL for leave management in PHP and MySQL [closed]

Let’s take it step-by-step… First, the entities you’re selecting are in the leave_request table. So let’s start there: SELECT leave_request.* FROM leave_request Now, you need to know the data for the applied_by column in the desired results. So you join the staff table: SELECT applied_staff.name AS applied_by FROM leave_request INNER JOIN staff AS applied_staff ON … Read more

[Solved] JOIN ON WITH MAX VALUE MYSQL

If you just want the max score per student you can aggregate the rows before joining: select s.id_student, u.name, u.role, s.status, u.no_phone, s.exam_status, sc.score from student s join user u on u.username = s.id_student join ( select Max(score) as score, id_student from score group id_student )sc on sc.id_student = s.id_student where mod(s.id_student, 2) = 0 … Read more

[Solved] SQL query needed for a complex structure

Without a better understanding of the rules and data this is the best I can come up with. It is based on your first array example – SELECT `r`.* FROM `rule_attribute_value` `rav` INNER JOIN `rule` `r` ON `rav`.`rule_id` = `r`.`rule_id` INNER JOIN `rule_attribute` `ra` ON `rav`.`attribute_id` = `ra`.`attribute_id` WHERE (`rav`.`store_id` = 0 AND `ra`.`attribute_code` = … Read more

[Solved] MYSQL: Search for User ID in one table to search for data in 2 other tables, then show data from all 3 tables

SELECT * FROM section_user_map JOIN users USING (UID) JOIN verification USING (UID) WHERE SectionID = 65 AND CompleteDate BETWEEN ‘2012-05-09 12:00:00’ AND ‘2012-05-11 12:00:00’ See it on sqlfiddle. No UNION required. Outer join would only be required if you still want to return results for users who do not exist in one (or both) of … Read more

[Solved] What join should I use with MySQL? [closed]

There are a few small mistakes in your query. In the ‘From’ section you only need to use the first table, and you’ll need to tell the join which field from the first table matches which field in the second table. In the where you’ll only have to match one field to $savingsId This query … Read more