I just started learning SQL last night. I'm having trouble displaying data using multiple JOIN statements. The tables I have are:
Table: CUSTOMER
Contains CustomerID, Country, Last Name
Table: TRANS
Contains CustomerID, TransactionID, DateSold, WorkID
Table: WORK
Contains WorkID, Title, Description
Here's my query:
Select CUSTOMER.LastName, CUSTOMER.CustomerID, WORK.WorkID,
Description, Title
FROM CUSTOMER JOIN TRANS
ON CUSTOMER.CustomerID = TRANS.CustomerID
JOIN WORK
ON TRANS.WorkID = WORK.WorkID
WHERE DateSold = '11/17/2014'
GROUP BY CUSTOMER.CustomerID, TRANS.CustomerID, CUSTOMER.LastName,
WORK.WorkID, Title, DateSold, Description
Note that in the select statement, I've deliberately left out a few items that appear in the GROUP BY statement, just for the sake of this post. (Their inclusion in the SELECT statement doesn't cause program to execute properly.)
All that appears is the GROUP BY statement, but no actual data. Please help me with what I'm doing wrong. Thank you.
Use the STR_TO_DATE function in mysql to convert the date string.
WHERE DateSole = STR_TO_DATE('11/17/2014', '%m/%d/%Y')
You can join one table with two different table but you have to start from the common table.
For how you wrote the query you are saying to join CUSTOMER first with TRANS and then with WORK but the conditions are wrong for this situation (and it is not what you want to do).
Select CUSTOMER.LastName, CUSTOMER.CustomerID, WORK.WorkID,
Description, Title
FROM TRANS JOIN CUSTOMER
ON CUSTOMER.CustomerID = TRANS.CustomerID
JOIN WORK
ON TRANS.WorkID = WORK.WorkID
WHERE DateSold = '11/17/2014'
GROUP BY CUSTOMER.CustomerID, TRANS.CustomerID, CUSTOMER.LastName,
WORK.WorkID, Title, DateSold, Description
It is TRANS that you join first with CUSTOMER and then with WORK.
Related
I want to show datetime field separately. Using SELECT CONVERT(VARCHAR(10),DateField,101) as DatePart gives me errors.
This is my original code:
$sql = "SELECT product.name, orders.date, orders.time FROM orders INNER JOIN
product ON orders.product_id = product.id AND
orders.customer_id='$customer_id'";
First, I've changed my orders table date and time to just datetime, and now I want to show them separately. As it was working before.
Secondly, Is my join correct? I want to know what customer has bought so far?
You can use TIME and DATE functions. For your JOIN, the customer_id check should be in the Where because it's not linked to product. Furthermore, if you have two columns in different tables, you should always put the name of the table before so the program will know what column you are talking about (see here product.product_id).
SELECT product.name, DATE(orders._datetime), TIME(orders._datetime)
FROM orders
INNER JOIN product ON orders.product_id = product.product.id
WHERE orders.customer_id='$customer_id'
You can do this.
SELECT DATE_FORMAT('2017-11-13 11:10:09', '%Y-%m-%d') AS date,
DATE_FORMAT('2017-11-13 11:10:09', '%T') AS time;
Output is
date time
2017-11-13 11:10:09
Your join looks ok to me.
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)
working with mySql I would like to list all purchases that customers made on a specific cathegory of products.
So, I had 3 tables: customers (idCustomer, Name) , cathegories (idCategory, CategoryName) and orders (idOrder, idCustomer, idCathegory, Qty, Price)
But I want a listing with ALL of the customers.
Not only the one who bought that specific idCategory
I thought something like:
select sum(Orders.Qty), Customers.Name
from Orders
right join Customers on Orders.idCustomer = Customer.idCustomer
where Orders.idCategory = 'Notebooks'
group by Orders.idCategory
but this statement only lists the records for customers who exists in Orders table.
And I want all of them ( the one who didnt buy, with qty =0 )
thanks in advance
Most people find left join easier to follow than right join. The logic for left join is to keep all rows in the first table, plus additional information from the remaining tables. So, if you want all customers, then that should be the first table.
You will then have a condition on the second table. Conditions on all but the first table should be in the on clause rather than a where. The reason is simple: when there is no match, then the value will be NULL and the where condition will fail.
So, try something like this:
select sum(o.Qty) as sumqty, c.Name
from Customers c left join
Orders o
on o.idCustomer = c.idCustomer and
o.idCategory = 'Notebooks'
group by c.Name;
Finally, the group by should have a relationship to the select clause.
Try this query
select sum(Orders.Qty), Customers.Name
from Customers
right join Orders on Customer.idCustomer = Orders.idCustomer and Orders.idCategory = 'Notebooks'
group by Customers.Name
Forgive me if I get some terms wrong, still trying to learn advanced mySql queries. I am trying to export data from a ecommerce platform, and some of the data I need is in lookup tables, the issue is the one lookup table I have can have more than 1 value associated, and the system I need to get the data into requires a specific format for that column.
I am using zen cart as my source if that helps, and below is the query.
Select zp.products_id as id, zpd.products_name as name, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
LEFT JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;
What I need is theres a look up table that tells me what categories belong to each product, I know it's going to be another join, but I need it so if more than 1 category belongs to a product to put it in the same column, and join them with a # symbol, and its not the id's of the category, its the path, so I need to look up the category ID to another table to get the path.
So "category1/subcategory#category2" for example..
Thanks for any guidance.
EDIT: I still need to get the results merged as I am getting double the results I want, but I see a record for each category it's in with this..
Select zp.products_id as id, zpd.products_name as name, zcd.categories_name as categories, products_price_w as cost, products_price as price, zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
ORDER BY zp.products_id ASC;
What you are looking for is group_concat(). I think it would be something like this:
Select zp.products_id as id, zpd.products_name as name,
group_concat(zcd.categories_name separator '#') as categories,
products_price_w as cost, products_price as price,
zp.products_date_added as date_created, zp.products_image as thumbnail
FROM `zen_products` as zp
INNER JOIN `zen_products_description` as zpd ON zp.products_id = zpd.products_id
INNER JOIN `zen_products_to_categories`as zptc ON zp.products_id = zptc.products_id
INNER JOIN `zen_categories_description` as zcd on zptc.categories_id = zcd.categories_id
WHERE zp.products_status = 1
group by zp.products_id
ORDER BY zp.products_id ASC;
I have the following query:
SELECT
DISTINCT sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id
FROM
sites,
earnings
WHERE sites.site_id = earnings.site_id AND sites.site_id IN('8', '1666')
That query gives me very well the information asked. It returns two rows, one for site 8 and another for site 1666, with the information on them from those tables.
Now, I want that the cust_id number be used to select from another table (let's say table customers) where they are stored by id and where other info is such as name, last name, etc.
Basically what I need is to expand that query to extract customer name and last name from the table customers, using the ids obtained.
Same way you got the info from two tables. Add a comma, add the third table name, and add the relationship to your WHERE clause like you did with the first two tables.
SELECT
DISTINCT sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id,
customers.name,
customers.last_name
FROM
sites,
earnings,
customers
WHERE sites.site_id = earnings.site_id AND sites.site_id IN('8', '1666') AND customers.id = earnings.cust_id
I think it's clearer to write out the JOINs though:
SELECT
sites.site_id,
sites.site_name,
sites.site_url,
earnings.cust_id,
customers.name,
customers.last_name
FROM
sites
INNER JOIN
earnings
ON
earnings.site_id = sites.site_id
INNER JOIN
customers
ON
customers.id = earnings.cust_id
WHERE
sites.site_id IN (8, 1666)
GROUP BY
sites.site_id