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.
Related
I am trying to create a query that returns the firstname, lastname, email and key_value for each order of a certain product (id 49) but I can't quite configure the SQL query correctly to get each key for each customer.
SELECT C.email,
C.firstname,
C.lastname,
K.key_value
FROM ps_customer C
INNER JOIN ps_orders O on C.id_customer = O.id_customer
INNER JOIN ps_order_detail OD on O.id_order = OD.id_order
INNER JOIN ps_keymanager OD on O.id_order = OD.id_order
WHERE OD.product_id =49
you could try this.. But!.
I've no idea what the ps_keymanager is, but if your using it
im sure you can fit into the query below.. ;)
SELECT O.reference AS ORDERREF, C.firstname, C.lastname, C.email AS Email
FROM ps_customer C
INNER JOIN ps_orders O on C.id_customer = O.id_customer
INNER JOIN ps_order_detail OD on O.id_order = OD.id_order
WHERE OD.product_id = 846
This will list
Order Reference | Firstname | Surname | Email
WHERE product_id = (in my case 846)
I share the consultation that I use to obtain different information from the client, by order id :
SELECT pod.id_order, c.id_customer,
CONCAT(c.firstname, c.lastname) AS client,
c.firstname, c.lastname, pa.address1, pa.address2,
pa.city, pa.id_country,c.email, pa.postcode, pa.phone_mobile,
pa.phone, pc.iso_code AS country
FROM ps_orders po
LEFT JOIN ps_order_detail pod ON (po.id_order = pod.id_order)
LEFT JOIN ps_product pp ON (pod.product_id = pp.id_product)
LEFT JOIN ps_customer c ON (c.id_customer = po.id_customer)
LEFT JOIN ps_address pa ON (pa.id_customer = c.id_customer)
LEFT JOIN ps_country pc ON (pc.id_country = pa.id_country)
WHERE pod.id_order = 34549
You can add or remove the columns you need in your sql query.
I'm working with the Sakila sample database, and trying to get the most viewed film per country. So far I've managed to get the most viewed film of a certain country given its id with the following query:
SELECT
F.title, CO.country, count(F.film_id) as times
FROM
customer C
INNER JOIN
address A ON C.address_id = A.address_id
INNER JOIN
city CI ON A.city_id = CI.city_id
INNER JOIN
country CO ON CI.country_id = CO.country_id
INNER JOIN
rental R ON C.customer_id = R.customer_id
INNER JOIN
inventory I ON R.inventory_id = I.inventory_id
INNER JOIN
film F ON I.film_id = F.film_id
WHERE
CO.country_id = 1
GROUP BY
F.film_id
ORDER BY
times DESC
LIMIT 1;
I supose that I'll have to use this query or something similar in the FORM of another query, but I've tried it all I could think and am completely unable to figure out how to do so.
Thanks in advance!
I admit, this is a hell of a query. But well, as long as it works.
Explanation:
Subquery: almost the same as you already has. Without the WHERE and LIMIT. Resulting in a list of movie-count per country
Result of that, grouped per country
GROUP_CONCAT(title ORDER BY times DESC SEPARATOR '|||'), will give ALL titles in that 'row', with the most-viewed title first. The separator doesn't matter, as long as you are sure it will never occurs in a title.
SUBSTRING_INDEX('...', '|||', 1) results in the first part of the string until it finds |||, in this case the first (and thus most-viewed) title
Full query:
SELECT
country_name,
SUBSTRING_INDEX(
GROUP_CONCAT(title ORDER BY times DESC SEPARATOR '|||'),
'|||', 1
) as title,
MAX(times)
FROM (
SELECT
F.title AS title,
CO.country_id AS country_id,
CO.country AS country_name,
count(F.film_id) as times
FROM customer C INNER JOIN address A ON C.address_id = A.address_id
INNER JOIN city CI ON A.city_id = CI.city_id
INNER JOIN country CO ON CI.country_id = CO.country_id
INNER JOIN rental R ON C.customer_id = R.customer_id
INNER JOIN inventory I ON R.inventory_id = I.inventory_id
INNER JOIN film F ON I.film_id = F.film_id
GROUP BY F.film_id, CO.country_id
) AS count_per_movie_per_country
GROUP BY country_id
Proof of concept (as long as the subquery is correct): SQLFiddle
Hey I am trying to join 4 tables in sql, with an left join and an inner join.
Hospital table
HospitalID| Name| Province| Email|
Order table
OrderID| HospitalID| StaffID| DeliverID| Date| Time
Item table
ItemID| Type| Name| Quantity| Expiry_Date
OrderItem table
OrderItemID| OrderID| ItemID| Quantity
I attempted executing the following SQL query but I am getting error message and I don't know what I'm doing wrong.
SELECT Hospital.Name, Item.Type, OrderItem.Quantity
FROM Hospital
LEFT JOIN [Order]
ON Hospital.HospitalID=[Order].HospitalID
INNER JOIN (SELECT Item.Type
FROM Item
GROUP BY Item.Type)
OrderItem ON Item.ItemID = OrderItem.ItemID
;
There are few mistakes in the query, the syntax using [Order] is invalid in mysql, and then you need an alias for the inner query. Also OrderItem needs to be joined first with Order. Further more no need to use use subquery for join since you are not doing any aggregate part to get the data from Item. In mysql the query will look like below.
SELECT
h.Name,
i.Type,
oi.Quantity
FROM Hospital h
LEFT JOIN `Order` o ON o.HospitalID = h.HospitalID
INNER JOIN OrderItem oi on oi.OrderID = o.OrderID
INNER JOIN Item i
on i.ItemID = oi.ItemID ;
Note that Order is a reserved word so you need to backtick it as done in the query above
you can use below simple query-
SELECT
hosp.Name,
itm.Type,
oi.Quantity
FROM Hospital hosp
JOIN `Order` ord ON ord.HospitalID = hosp.HospitalID
JOIN OrderItem oi on oi.OrderID = ord.OrderID
JOIN Item itm on itm.ItemID = oi.ItemID;
If there is chance that you want to show all hospital name even without its details in other tables like type, quantity etc. then you can use of left join-
SELECT
hosp.Name,
itm.Type,
oi.Quantity
FROM Hospital hosp
LEFT JOIN `Order` ord ON ord.HospitalID = hosp.HospitalID
LEFT JOIN OrderItem oi on oi.OrderID = ord.OrderID
LEFT JOIN Item itm on itm.ItemID = oi.ItemID;
I have 3 tables
table_student
ID, firstname, lastname
table_courses
ID, course_name
table_student_course
ID, student_ID, course_ID, date_taken
All I want to do is list all courses by course_name and date_taken by student with ID=1
Anyone please?
You need to use the JOIN on the table_courses and table_student_course tables and then apply the Order By on cource_name to sort out by course name.And for selecting the particular student apply the Where clause as filter.
SELECT
t.course_name,
tsc.date_taken
FROM
table_courses t INNER JOIN table_student_course tsc
ON t.ID = tsc.course_ID
WHERE
tsc.student_ID = 1
ORDER BY
t.course_name
If you also want to get the students detail from the query then you need to join the 3 tables like below,
SELECT s.firstname, s.lastname, c.course_name, sc.date_taken
FROM table_courses c
INNER JOIN table_student_course sc ON c.ID = sc.course_ID
INNER JOIN table_student s ON sc.student_ID = s.ID
WHERE sc.student_ID = 1
ORDER BY c.course_name
SELECT s.firstname, s.lastname, c.course_name, sc.date_taken
FROM table_courses c
INNER JOIN table_student_course sc ON c.ID = sc.course_ID
INNER JOIN table_student s ON sc.student_ID = s.ID
WHERE sc.student_ID = 1
ORDER BY c.course_name
By using Inner Join on the tables "table_courses" & "table_student_course" we selects all rows from both tables as long as there is a match between the columns in both tables making sure the ids are the same. If the condition is met( student_id=1) then the query will return what is expected.
SELECT course_name, date_taken
FROM table_courses c INNER JOIN table_student_course sc ON c.id = sc.course_id
WHERE sc.student_ID = 1
ORDER BY course_name
I'm trying to get the total rental amount for each client from Sakila example database
so I tried with the following query:
select customer.customer_id, customer.first_name,
(select sum(payment.amount) from customer
inner join rental on customer.customer_id=rental.customer_id
inner join payment on rental.rental_id=payment.rental_id group by payment.amount)
from customer
inner join rental on customer.customer_id=rental.customer_id
inner join payment on rental.rental_id=payment.rental_id
group by customer.customer_id;
and I get this "Subquery returns more than one row". Do you know what could be wrong? Thank you
This is your query, with some reformatting and the use of table aliases:
select c.customer_id, c.first_name,
(select sum(p2.amount)
from customer ce inner join
rental r2
on c2.customer_id = r2.customer_id inner join
payment p2
on r2.rental_id = p2.rental_id
group by p2.amount
-------^
)
from customer c inner join
rental r
on c.customer_id = r.customer_id inner join
payment p
on r.rental_id = p.rental_id
group by c.customer_id;
I've highlighted the specific cause of your problem. But the fix is to radically simplify the query:
select c.customer_id, c.first_name, sum(p.amount)
from customer c left join
rental r
on c.customer_id = r.customer_id left join
payment p
on r.rental_id = p.rental_id
group by c.customer_id;
Is that the result you're looking for?
SELECT C.customer_id
,C.first_name
,SUM(P.amount) AS [total_amount]
FROM customer C
INNER JOIN rental R ON R.customer_id = C.customer_id
INNER JOIN payment P ON P.rental_id = R.rental_id
GROUP BY C.customer_id, C.first_name
-- Condition to get only the largest amount
-- without using an ORDER BY clause
HAVING SUM(P.amount) = (SELECT MAX(SUM(P2.amount))
FROM rental R2
INNER JOIN payment P2 ON P2.rental_id = R2.rental_id
GROUP BY R2.customer_id)
Hope this will help you.