select
first_name,
last_name,
c.name as company_name,
sc.`date` as screening_date
from
guests g
inner join
user_guest_group ugs on ugs.guest_id = g.id
inner join
companies c on c.id = g.company_id
inner join
screening_date_guest sdg on sdg.guest_id = g.id
inner join
screening_dates sc on sc.id = sdg.screening_date_id
where
sdg.attending = 1
and
screening_date_id = 1
group by
first_name,
last_name
Results:
Peter, M, Bell Media (ctv), 2015-05-18 00:00:00
Adam, D, Highway Entertainment, 2015-05-18 00:00:00
Todd, F., Multichoice, 2015-05-18 00:00:00
John, D, Talpa, 2015-05-18 00:00:00
Maria, F, UK TV, 2015-05-18 00:00:00
John, L, WBDTD, 2015-05-18 00:00:00
Albert, P, WBDTD, 2015-05-18 00:00:00
My query returns that resulset.
Now, I want to see another column with total guests per company.
In this case, we have 2 guests from WBTDT so it should say total_guest = 2
Can someone help me ?
Thanks
One way to do this is to get the count per company in a correlated subquery, so maybe this is what you want?
select
first_name,
last_name,
c.name as company_name,
sc.date as screening_date,
(
select count(*) from guests
inner join
user_guest_group on user_guest_group.guest_id = guests.id
inner join
companies on companies.id = guests.company_id
inner join
screening_date_guest on screening_date_guest.guest_id = guests.id
inner join
screening_dates on screening_dates.id = screening_date_guest.screening_date_id
where
screening_date_guest.attending = 1
and
screening_date_id = 1 and company_id = c.id
) total_guests
from
guests g
inner join
user_guest_group ugs on ugs.guest_id = g.id
inner join
companies c on c.id = g.company_id
inner join
screening_date_guest sdg on sdg.guest_id = g.id
inner join
screening_dates sc on sc.id = sdg.screening_date_id
where
sdg.attending = 1
and
screening_date_id = 1
group by
first_name,
last_name,
c.id,
c.name,
sc.date
select
first_name,
last_name,
c.name as company_name,
sc.`date` as screening_date,
count(g.id)
from
guests g
inner join
user_guest_group ugs on ugs.guest_id = g.id
inner join
companies c on c.id = g.company_id
inner join
screening_date_guest sdg on sdg.guest_id = g.id
inner join
screening_dates sc on sc.id = sdg.screening_date_id
where
sdg.attending = 1
and
screening_date_id = 1
group by
first_name,
last_name,
c.name
Related
There's my sql data:
id name prod_id price
------------------------
9 A 23 4100
94 B 40 1500
94 B 36 1500
I would ideally like to finally have:
id name prod_id price
------------------------
9 A 23 4100
94 B 40,36 1500
Here's my code I've tried using GROUP BY
SELECT
company.id,
company.name,
order_product.product_id,
SUM(orders.price)
FROM
orders
INNER JOIN users ON orders.user_id = users.id
INNER JOIN order_product ON orders.id = order_product.order_id
INNER JOIN customer ON orders.customer_id = customer.id
INNER JOIN company ON customer.company_id = company.id
GROUP BY
company.id,
group_concat should do the trick
SELECT
company.id,
company.name,
group_concat(order_product.product_id9,
orders.price
FROM
orders
INNER JOIN users ON orders.user_id = users.id
INNER JOIN order_product ON orders.id = order_product.order_id
INNER JOIN customer ON orders.customer_id = customer.id
INNER JOIN company ON customer.company_id = company.id
group by
company.id,
company.name,
orders.price
created this query but all the customers in the table are not showing up, i want all customers to show in the query even with any null values.there are 26 customers in the customer table but only 12 show when i run the query.
SELECT c.CID, CONCAT(c.FirstName, ' ', c.LastName) AS 'Customer Name',
c.Email, o.OrderDate, o.OrderStatus, o.DeliveryDate, a.AppetizerName, o.AppetizerQuantity,
p.PizzaName, p.PizzaSize, o.PizzaQuantity, pa.PastaName, o.PastaQuantity,
s.SandwichName, o.SandwichQuantity, d.DessertName, o.DessertQuantity,
dr.DrinkName, o.DrinksQuantity, o.TotalPrice
FROM
customer AS c
JOIN
orders AS o ON c.CID = o.CID
INNER JOIN
appetizer AS a ON a.AppetizerID = o.AppetizerID
INNER JOIN
pizza AS p ON p.PizzaID = o.PizzaID
INNER JOIN
pasta AS pa ON pa.PastaID = o.PastaID
INNER JOIN
sandwiches AS s ON s.SandwichID = o.SandwichID
INNER JOIN
dessert AS d ON d.DessertID = o.DessertID
INNER JOIN
drinks AS dr ON dr.DrinkID = o.DrinkID
GROUP BY c.FirstName, c.LastName;
Your issue is that you're using inner joins. These drop null values. If you replace each of your inner joins with left joins, your query should return all 26 customers.
SQL CODE:
select student.sex, class.date
from ((student INNER JOIN student_course ON student.name=student_course.Student)
INNER JOIN class ON student_course.Course=class.Course);
Result:
sex date
m 25.2.19
m 27.2.19
m 27.2.19
m 27.2.19
m 25.2.19
m 27.2.19
m 25.2.19
f 25.2.19
f 27.2.19
f 27.2.19
f 25.2.19
f 26.2.19
Now my target is to get the date where both male and female student appear. So
I modified the code to be:
SELECT o.date
FROM ( select student.sex, class.date
from ((student INNER JOIN student_course ON student.name=student_course.Student)
INNER JOIN class ON student_course.Course=class.Course)) AS o
WHERE o.sex='m' AND o.sex='f';
The result is NULL. But I want the result as:
date
25.2.19
27.2.19
You can do JOIN with aggregation, no any sub-query needed :
select c.date
from student s inner join
student_course sc
on s.name = sc.Student inner join
class c
on sc.Course = c.Course
where s.sex in ('F', 'M')
group by c.date;
This would display dateS where it founds male or female, however your sample output suggests :
select c.date
from student s inner join
student_course sc
on s.name = sc.Student inner join
class c
on sc.Course = c.Course
where s.sex in ('F', 'M')
group by c.date
having count(distinct s.sex) = 2;
If you want dates with both male and female students, use aggregation:
select c.date
from student s INNER JOIN
student_course sc
ON s.name = sc.Student INNER JOIN
class c
ON sc.Course = c.Course
group by c.date
having min(s.sex) = 'F' and max(s.sex) = 'M';
If you have other genders, then you might want to filter on where s.sex in ('F', 'M') for the having logic to work.
Correct answer by #Yogesh Sharma
SQL CODE:
select c.date
from student s inner join
student_course sc
on s.name = sc.Student inner join
class c
on sc.Course = c.Course
where s.sex in ('F', 'M')
group by c.date
having count(distinct s.sex) = 2;
i have follwing tables
USERS :
* id
* name
* role id
* phone
STUDENTS
1: userid ( fk users.id)
2: course_id
STAFF
1: staff_id(fk users.id)
2: course-id
COURSES
1: COURSE_ID
1:COURSENAME
I need to find all accounts associated with a number
I need name,userid,roleid,courseid,corsename,staffid, by providing mobile
i have written this query but this return zero results
SELECT users.name, users.id, staff.user_id, students.course_id, users.role_id, courses.course_title FROM users INNER JOIN students ON users.id = students.user_id INNER JOIN staff ON staff.user_id = users.id INNER JOIN courses ON courses.id = students.course_id WHERE users.phone = '9495990028'
LIMIT 0 , 30
Try union operator:
SELECT name, s.userid, roleid, s.course_id, c.coursename, null
FROM USERS u
INNER JOIN STUDENTS s
INNER JOIN COURSES c
ON s.userid = u.id AND c.course_id = s.course_id
WHERE u.phone = ?
UNION
SELECT name, null, roleid, s.course_id, c.coursename, s.staff_id
FROM USERS u
INNER JOIN STAFF s
INNER JOIN COURSES c
ON s.staff_id = u.id AND c.course_id = s.course_id
WHERE u.phone = ?;
For the COURSES table, I check the id with the course_id from students and staff
SELECT name, s.userid, roleid, s.course_id, c.coursename, st.staff_id
FROM USERS u
INNER JOIN STUDENTS s ON s.userid = u.id
INNER JOIN STAFF st ON st.staff_id = u.id
INNER JOIN COURSES c ON c.course_id = s.course_id AND c.course_id = st.course_id
WHERE u.phone = ?
I have a query like this:
SELECT c.id, c.name, f.name
FROM companies c
INNER JOIN facilities f ON c.id = f.company
ORDER BY c.name DESC, f.name
I also want to retrieve a COUNT() of all work_orders (a table) that are approved (a column containing 0 or 1) for each row (each facility).
e.g, SELECT COUNT(*) FROM work_orders w WHERE w.facility = f.id AND w.approved = 1
The result should look like
company | facility | count
--------------------------
goog | ohio | 2
goog | cali | 0
tekk | cupertin | 0
As kind of a follow up, i'd also like to add another count column where w.approved = 0
SELECT c.id, c.name, f.name, COUNT(w.id) AS work_orders
FROM companies c
INNER JOIN facilities f ON c.id = f.company
-- LEFT JOIN used in case there are facilities with no work orders
LEFT JOIN work_orders w ON f.id = w.facility AND w.approved = 1
GROUP BY c.id, c.name, f.name
ORDER BY c.name DESC, f.name
To do multiple counts (approved or not):
SELECT c.id, c.name, f.name,
wapp.wo AS approved_work_orders,
wnapp.wo AS non_approved_work_orders,
FROM companies c
INNER JOIN facilities f ON c.id = f.company
LEFT JOIN (SELECT facility, COUNT(*) AS wo FROM work_orders WHERE approved=1 GROUP BY facility) wapp ON f.id = wapp.facility
LEFT JOIN (SELECT facility, COUNT(*) AS wo FROM work_orders WHERE approved=0 GROUP BY facility) wnapp ON f.id = wnapp.facility
ORDER BY c.name DESC, f.name
It looks like you just need a grouping on c.id and an if statement
SELECT c.id, c.name, f.name, IF(count(0)>0,1,0)
FROM companies c
INNER JOIN facilities f ON c.id = f.company
LEFT JOIN work_orders w ON w.facility = f.id
GROUP BY f.id
ORDER BY c.name DESC, f.name