Nested SQL query? - mysql

I have 2 tables like this:
product_table[ product_id, product_name,original_store_id, destination_store id]
and
store_table[ store_id, establishment_date, location]
What I want to find is: From all those products that are shipped TO stores with no establishment date, how many of them are shipped FROM stores with establishment date?
This is my query:
SELECT count(a.product_id) as count_of_products
FROM product_table a
JOIN store_table b
ON a.original_store_id = b.store_id AND a.destination_store_id = b.store_id
WHERE b.establishment_date IS NULL
I understand this should be a nested query, but how do I put them here?

You may try adding a second join to the store_table table to further restrict to only products shipped from stores with an establishment date:
SELECT COUNT(a.product_id) AS count_of_products
FROM product_table a
INNER JOIN store_table b
ON a.destination_store_id = b.store_id
INNER JOIN store_table c
ON a.original_store_id = c.store_id
WHERE
b.establishment_date IS NULL AND
c.establishment_date IS NOT NULL;

Related

mySQL - inner join three tables

The final question on my database. I have triad several approaches to get the receipt for the customer for the transaction in my DB.
customer_tbl - customer_ID PK
transaction_tbl - customer_ID FK
Payment_tbl - customer_id FK
I wonder if you could please help and based on that I will add game title, rental date, return date and total payment?
The JOIN should take the form of:
SELECT ... FROM customer_tbl c
JOIN transaction_tbl t on c.customer_id = t.customer_id
JOIN Payment_tbl p ON t.payment_id = p.payment_id;
If you want more details, you'll have to provide more information from your side.
Found it:
SELECT t.* , p.* FROM transaction_tbl t INNER JOIN payment_tbl p
ON p.payment_id = t.payment_id WHERE t.customer_id = 1;
This solves my transaction report. I
SELECT t.* , g.game_name , p.*
FROM transaction_tbl t
INNER JOIN game_tbl g ON g.game_id = t.game_id
INNER JOIN payment_tbl p ON p.payment_id = t.payment_id
WHERE t.customer_id = 1;
It works for any given customer (in this case '1') and it selects FKs from transaction_tbl and dates. From customer_tbl you have only customer _id (similar to shops where you get only your loyalty card number) and payment details from payment_tbl. Learned a lot and thanks all for your help.

Mysql query involving multiple tables and an OR statement

Sorry in advance for an ambiguous title, but I can't think of a good one.
I have 2 tables:
bookings(booking_id, customer_id)
charges(customer_id, booking_id, amount)
where in charges table, either booking_id or customer_id must be entered. Not both.
I'm trying to get amount that are associated with a customer_id
Because the customer_id's are null sometimes in charges table, I have to use booking_id to acquire customer_id through bookings table.
So, I had a query like this:
SELECT c.amount
FROM charges as c, bookings as b
WHERE (c.customer_id = 1234) OR
(c.customer_id = null AND c.booking_id = b.booking_id AND b.customer_id = 1234)
However, it seems to create an infinite loop.
Any suggestions?
Thanks,
The problem is that you are doing a cross join (cartesian product), based on the structure of your where clause.
A better approach is to use left outer join (and proper join syntax). So, join to the bookings table, if it exists. Then use or in the where clause to look for a match on the customer_id in either place:
SELECT c.amount
FROM charges as c left outer join
bookings as b
on c.booking_id = b.booking_id
WHERE (c.customer_id = 1234) OR (b.customer_id = 1234)
Why you don't use a JOIN ? like
SELECT c.amout
FROM charges AS c
LEFT JOIN bookings AS b ON c.bookin_id = b.booking_id
WHERE c.customer_id = 1234 OR b.customer_id = 1234
SELECT amount, customer_id from charges where customer_id = 1234
UNION
select amount, b.customer_id from charges c, bookings b where b.booking_id = c.booking_id and c.customer_id is null and b.customer_id = 1234

mysql select issue, multi tables

Whilst I've asked a question similar to this before, I've attempted to apply the same technique to it and its just not working as it should and erroring and various points.
I've created an sqlfiddle for this; http://sqlfiddle.com/#!2/b1a29
I'm attempting to create a select function that will return the animal_id, animal_name,animal_type_name, shelter_name, animal_type_id and location_name.
I've attempted to have a good at it with the following code but clearly I'm missing something;
$query = $this->db->query('SELECT animal_id, animal_name, animal_type_name, shelter_name, shop_id, location_name
FROM animals a
INNER JOIN shelter s ON s.shop_id = a.shop_id
INNER JOIN location l ON l.location_id = s.location_id
INNER JOIN animal_types at ON at.animal_type_id = a.animal_type_id');
The problem is that you are selecting the column name which is ambiguous or which exists on multiple tables. In this case it's the shop_id. In order to execute the query well, you need to specify where the column shop_id should come from,
SELECT animal_id, animal_name, animal_type_name,
shelter_name, s.shop_id, location_name
FROM animals a
INNER JOIN shelter s ON s.shop_id = a.shop_id
INNER JOIN location l ON l.location_id = s.location_id
INNER JOIN animal_types at ON at.animal_type_id = a.animal_type_id
SQLFiddle Demo
When I run your code, it generate error:
Column 'shop_id' in field list is ambiguous: SELECT animal_id,
animal_name, animal_type_name, shelter_name, shop_id, location_name
FROM animals a INNER JOIN shelter s ON s.shop_id = a.shop_id INNER
JOIN location l ON l.location_id = s.location_id INNER JOIN
animal_types at ON at.animal_type_id = a.animal_type_id
From the error: your shop_id column belongs to more one table, so you must qualified it with table name / table alias.
Change your query to this :
SELECT animal_id, animal_name, animal_type_name, shelter_name, s.shop_id, location_name
FROM animals a
INNER JOIN shelter s ON s.shop_id = a.shop_id
INNER JOIN location l ON l.location_id = s.location_id
INNER JOIN animal_types at ON at.animal_type_id = a.animal_type_id

SQL query for 3 tables

I have 3 tables as follows
item{id, name, price}
customer{id, name, tel_no}
order{id, time, customer_id}
order_item{id, item_id, price, order_id}
process{id, order_item_id, status}
I need to get order_items which are not processed for a particular customer. I tried with the following query. But it doesn't help. Please correct me some one.
SELECT *
FROM order_item`
INNER JOIN `order` ON `order`.id = order_item.order_id
WHERE `order`.customer_id=1 AND NOT EXISTS (
SELECT *
FROM process
WHERE process.order_item_id=order_item.id
)
I'm using mysql as my server
Select *
from order_item OI
INNER JOIN ORDER O on O.ID=OI.Order_Id
LEFT JOIN Process P ON P.Order_item_ID = OI.Item_ID
where O.Customer_ID = 1 and P.ID is null
LEFT JOin gives you all the ORDER_Items and only those records with a matching process record so P.ID will be null thus the item has not been processed

How to create SQL subquery ON JOIN using multiple tables

I have the folowing tables.
ORDER
OrderNumber
CustomerNumber
EmployeeNumber
OrderDate
CUSTOMER
CustomerNumber
Name
Address
EMPLOYEE
EmployeeNumber
Name
Address
ORDERDETAIL
OrderNumber
Qty
Description
Price
Let say ORDERDETAIL table has 10 records
I would like to write a query that will return 10 records from ORDERDETAIL table to include Employee name, employee address, customer name, customer address and and order Date.
I know that I could write a query and use INNER JOIN to get the info from ORDER table, but how do you create the rest of query to get the info from the CUSTOMER and EMPLOYEE tables.
SELECT *
FROM OrderDetail D
INNER JOIN Order O
ON D.OrderNumber = O.OrderNumber;
Just add some more joins...
SELECT *
FROM OrderDetail D
JOIN Order USING (OrderNumber)
JOIN Customer USING (CustomerNumber)
JOIN Employee USING (EmployeeNumber)
You might want to re-order the JOINs in order to have the smallest tables first, as this could provide you with some performance boost (depending on your server's version, the most recent will optimize the join for you and might actually execute the joins in the "probably best" way).
Also, in the MySQL dialect at least, JOIN implicitly expands to INNER JOIN, and writing
A JOIN B USING (COL)
is equivalent to writing
A JOIN B ON (A.COL = B.COL)
SELECT *
FROM OrderDetail D
INNER JOIN Order O ON D.OrderNumber = O.OrderNumber
INNER JOIN Eployee E on O.EployeeNumber = E.EployeeNumber
INNER JOIN Customer C on O.CustomerNumber = C.CustomerNumber
if you have foreign key reference between
ORDER.EmployeeNumber and EMPLOYEE.EmployeeNumber
ORDER.CustomerNumber and CUSTOMER.CustomerNumber
then try this
SELECT
E.name AS employeeName,
E.Address AS employeeAddress,
C.name AS customerName,
C.Address AS customerAddress.
O.OrderDate
FROM OrderDetail D
INNER JOIN Order O ON D.OrderNumber = O.OrderNumber
INNER JOIN EMPLOYEE E ON E.EmployeeNumber = 0.EmployeeNumber
INNER JOIN CUSTOMER C ON C.CustomerNumber= 0.CustomerNumber
LIMIT 0,10