How to Join the two values in the same table in sql? - mysql

- link for the sample table
I have a table name as Customer. In that table, there are several attributes like customer_id, customer_name, address, country, bill_to_customer, account_start_date, etc...So I want to get the billing customer name, address, country, from that table. In here, customer_id may be equal to bill_to_customer or customer_id not equal to bill_to_customer_id. When I want to get the billing customer details, what it does is, go to bill_to_customer value and filter the data of customer_id which matches with the bill_to_customer and give the output. How to write a SQL query to do this?

you can do a self join on table Customer with your bill_to_customer column.
Something like this:
SELECT billing.name, billing.address, billing.country FROM Customer cust
INNER JOIN Customer billing ON cust.bill_to_customer = billing.customer_id
WHERE ...further conditions...

Related

Adding customers name alongside customer name

based on the DB schema I am trying to get for each customer the Id, date, customer name and the customer. Can anyone help?
I am struggling with adding the name of the customer. This is what I have tried so far:
SELECT customer,name,date
from table
You need two joins to the customers table. That said, you really need to learn to use meaningful table aliases rather than arbitrary letters. Table aliases are your friend, and arbitrary letters aren't so friendly.
So:
SELECT i.Id, i.BillingDate, c.Name as CustomerName,
cr.Name as ReferredByName
FROM Invoices i LEFT JOIN
Customers c
ON i.CustomerId = c.Id LEFT JOIN
Customers cr
ON i.ReferredBy = cr.Id;

MYSQL joining tables and selecting customer with no order for particular date

I am having trouble with this mysql query. I have a customer table and an orders table, linked by a customer ID. The orders table has a date field and I am trying to list the names of customers that do not have any orders for a particular month, July 2016. I was told a NOT IN might help but I'm not sure how to go about it. I looked for similar examples but they use IS NULL. I tried this but it did not like the NOT (not a valid input at this position:
SELECT customer.cust_name
FROM customer
LEFT JOIN ordertbl ON ordertbl.cust_id = customer.cust_id
WHERE order_date like '2016-07%' not in ordertbl.order_date;
I then tried this but it returned no results:
SELECT customer.cust_name
FROM customer
LEFT JOIN ordertbl ON ordertbl.cust_id = customer.cust_id
WHERE (SELECT COUNT(order_date like '2016-07%')) IS NULL;
I also found a similar example but couldn't get it, no results:
Select customer.cust_name
From customer
where ordertbl.cust_id Not in (
Select customer.cust_id
From ordertbl
Where ordertbl.order_date like '2016-07%');
I'm sure I'm going about this all wrong. I tried a few other examples but those didn't work either. Any help is appreciated.
Assuming that in the orders table, your customer ID reference is called CUSTOMER_ID, the query is:
SELECT CUSTOMER.CUST_NAME
FROM CUSTOMER
WHERE CUST_ID IN
(SELECT ORDERTBL.CUSTOMER_ID
FROM ORDERTBL
WHERE DATE <> yourdate)

Calculate in SQL with Inner Join

I am trying to calculate in a SQL statement. I am trying to calculate the total amount in the invoice.total column per customer. I created the following statement:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id;
When I run this, I get the total amount in the table. I have 15 different customers in this table but I get only the name of the first customer and the total amount of all customers. What am I doing wrong?
First, you need to Group the data when you want to have aggregate results:
SELECT customers.firstname, customers.lastname, customers.status, SUM(invoice.total) AS total
FROM customers
INNER JOIN invoice
ON customers.id=invoice.id
GROUP BY customers.firstname, customers.lastname, customers.status;
Second, are you sure you are joining the table by correct fields? Is invoice.id correct column? I would expect invoice.id to be primary key for the table and instead I would expect another column for foreign key, invoice.customerid for example. Please double check it is correct.
UPDATE: As it was mentioned in comments, if you have two customers with same first name, last name and status, the data will be grouped incorrectly. In that case you need to add unique field (e.g. customers.id) to SELECT and GROUP BY statements.
You have to add a group by customer (id property for example). If you want to have first and last name in select then you will have to group by them as well.
Use group by clause in your query or you can use common table expression to get the sum of records by customer and then make a inner join with customer table.
You need to add a Group By with column names in your query.

How can I grab information in MySQL via 2 foreign keys going to the same table?

I have a table called sales with two columns vendor_id and customer_id. Both are id's to a row in the people table. I want to grab a sales row by it's id and get the customer name and the vendor name.
How can I do this with MySQL?
Join your people table twice to the sales table (you'll need to alias it at least one time in order to avoid a name collision):
SELECT customer.name AS customer_name, vendor.name AS vendor_name
FROM sales
JOIN people AS customer ON customer.id = sales.customer_id
JOIN people AS vendor ON vendor.id = sales.vendor_id
WHERE sales.id = ?

Return records when table is not empty

I am brand new to SQL and can only do the most basic of SQL queries and appreciate any help with this problem.
MySQL database contains a CUSTOMER table and a PURCHASES table. If the customer has made a purchase there will be one or more serial numbers and a purchase date in the PURCHASES table.
I would like to know how to return the customer name, serial number and date of purchase for each customer who has made a purchase between two dates.
Here is a description of the tables:
PURCHASES
customer_id
serial_number
purchase date
CUSTOMER
customer_id
customer_name
customer address
Here it is:
select c.customer_name, p.serial_number, p.purchase
from purchases p, customers c
where p.customer_id = c.customer_id and
p.purchase between '2012-08-27' and '2012-08-31'
The SQL Fiddle is here so that you can play with it: http://sqlfiddle.com/#!2/e8002/3
You need to JOIN the two tables; see MySQL documentation for the joining concept.
You need to limit the query on purchases between the two dates:
WHERE purchase BETWEEN 'date1' AND 'date2'
and this will tell you the customer ID. With this you select into the Customers table, so:
SELECT customer.customer_name,
purchases.serial_number,
purchases.purchase
FROM customer
JOIN purchases ON (customer.customer_id = purchases.purchase_id)
WHERE purchases.purchase BETWEEN '2012-01-01' AND '2012-08-01';