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.
Related
I'm trying to create a SQL query where I get the results of what Accounts have had no Orders for a given Business.
So I want to filter on the Business level, and there can be multiple businesses and all have unique accounts to them.
The database relationship could basically be looked at like this:
I've tried this with a test business account, however, I'm not getting the results I wanted:
SELECT DISTINCT(A.name)
FROM Accounts AS A
LEFT JOIN `Token` AS T ON T.account_id = A.id
WHERE NOT EXISTS (SELECT 1 FROM Orders AS O WHERE O.account_id = A.id)
AND T.business_id = 1
I was also hoping I could just do a WHERE check on the Orders if business_id = 1, but that didn't give me anything different.
Why do you need the Token table in this query? If you only want accounts with no orders then you could do it like this:
SELECT A.name FROM Accounts A
WHERE NOT EXISTS( SELECT O.id FROM Orders O WHERE O.account_id = A.id AND O.business_id = 1 )
Hello You can test this:
SELECT A.name
FROM Accounts AS A
LEFT JOIN Orders AS O ON A.id = O.account_id
LEFT JOIN Businesses AS B ON B.id = O.business_id
GROUP BY A.name
HAVING COUNT(O.id) = 0;
I want sql query for this . I have three tables customer,account, transaction
the schema of the are as below
I want to get customer details, account id, sum of transaction id
Your query is quite close. Try this:
SELECT c.customer_id, a.Account_id, SUM(t.transaction_amount) as amount
FROM Account a INNER JOIN
Customer c
ON a.customer_id = c.customer_id INNER JOIN
Transaction t
ON a.account_id = t.account_id
GROUP BY c.customer_id, a.account_id;
Note the use of table aliases to simplify the query. And -- more importantly -- the SELECT columns and GROUP BY columns are consistent.
Because the customer_id is in the Account table, you don't need the Customer table. So, you can simplify this to:
SELECT a.customer_id, a.Account_id, SUM(t.transaction_amount) as amount
FROM Account a INNER JOIN
Transaction t
ON a.account_id = t.account_id
GROUP BY a.customer_id, a.account_id;
You can Sum the transactions grouped by CustomerID and then join to the customers table on the CustomerID like this:
SELECT c.Customer_name, c.Customer_mobile, t.Transaction_sum
FROM Customer c
JOIN (
SELECT t.CustomerID, SUM(t.Sum) as Transaction_sum
FROM transactions t
GROUP BY t.CustomerID
) t on t.CustomerID = c.CustomerID
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;
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
I need help writing a MySQL query. So far, none of the questions out there seem to fit my needs.
I have an order table and an order_log table. In the order_log table I make a record every time an order's status is changed. I need to display a list of the most recent status changes from the order_log table. The query I'm currently using does a JOIN on the two tables and grabs everything where order.status = order_log.status.
The problem with this is that some times an order will pass through the same status more than once. When that occurs, my query grabs every entry in the order_log table for that order and that status, but I only want the most recent log.
I tried writing a new JOIN query to grab the Max of the order_log date entry, but it's only returning 1 entry. This is what I have.
SELECT *
FROM order_status_log AS l
JOIN orders AS o ON ( l.order_id = o.id )
WHERE l.status = o.status
AND l.date = (
SELECT MAX( date )
FROM order_status_log l2
JOIN orders AS o2 ON ( l2.order_id = o2.id )
)
Any ideas?
there are many ways to do it, one is to have a separate subquery the gets the latest entry of each record: order_ID.
The result of the subquery is then joined back with the original table but has multiple conditions: that it matches the order_ID and also the latest date.
SELECT a.*, b.*
FROM `order` a
INNER JOIN order_log b
ON a.id = b.order_ID
INNER JOIN
(
SELECT order_ID, MAX(date) max_date
FROM order_log
GROUP BY order_ID
) c on b.order_ID = c.order_ID AND
b.date = c.max_date
That may help;
select olg.column1,o.column2,max(olg.date) from --You can add other columns as well
order_status_log olg
join orders o
on olg.id = o.order_id
group by olg.column1,o.column2