I have multiple tables as follows:
TABLE 1: Product
+-----+----------+--------+---------------------+
| id | biz_id | name | message |
+-----+----------+--------+---------------------+
| 1 | 1 | test1 | One tow three |
| 2 | 1 | test1 | One tow three |
| 3 | 1 | test1 | One tow three |
| 4 | 2 | test2 | hello world |
| 5 | 2 | test2 | hello world |
+-----+----------+--------+---------------------+
TABLE 2: Images
+-----+----------+--------------+-------------------+
| id | biz_id | product_id | path |
+-----+----------+--------------+-------------------+
| 1 | 1 | 1 | img/qwert1.jpg |
| 2 | 1 | 2 | img/qwert2.jpg |
| 3 | 1 | 3 | img/qwert3.jpg |
| 4 | 2 | 4 | img/qwery4.jpg |
| 5 | 2 | 5 | img/qwert5.jpg |
+-----+----------+--------------+-------------------+
How can I avoid duplicate in mysql while joining multiple tables?
My Query is Join both tables such that I want to avoid duplicate product(Get Distint product by name) and get all images associated with that product(Eg. Product>name - test1 has images qwert1.jpg, qwert2.jpg, qwert2.jpg )
SELECT p.name, GROUP_CONCAT(i.path)
FROM product AS p
INNER JOIN images AS i ON p.id = i.product_id
GROUP BY p.name;
Please note that this is not standard SQL, but most DBMS offer this functionality (although the keyword may be a bit different).
simple join should do all things
select p.name as Name, i.message as IMG from Images
left join Product p
on i.biz_id=p.biz_id
group by p.name;
use left join with GROUP_CONCAT or group by
select p.name name,GROUP_CONCAT(i.path) img
from Images i left join Product p
on i.biz_id=p.biz_id
group by i.product_id
Related
I'm trying to create a query that would basically be the equivelant of this (does not work).
SELECT * FROM `categories` AS C AND
SELECT * FROM `items` AS I AND
SELECT COUNT(I.id) AS items AND
SELECT SUM(I.price) AS price;
I am not using SQLServer, and I'm using PDO through PHP for database connectivity.
Here's the tables.
Category
+----+------------+
| id | Category |
+----+------------+
| 1 | First_Cat |
| 2 | Second_Cat |
+----+------------+
items
+----+----------+------+-------+
| id | category | name | price |
+----+----------+------+-------+
| 1 | 1 | Foo | 1.99 |
| 2 | 1 | Bar | 2.00 |
| 3 | 2 | ooF | 0.99 |
| 4 | 2 | raB | 1.99 |
+----+----------+------+-------+
Based on these tables I would be expecting these query results:
+----+------------+-------+-------+--+
| id | category | items | price | |
+----+------------+-------+-------+--+
| 1 | First_Cat | 2 | 3.99 | |
| 2 | Second_Cat | 2 | 2.98 | |
+----+------------+-------+-------+--+
Any help?
The query that you have posted in the comment you are not joining the category table and the item table. That makes me think that you could do something like this?:
SELECT
categories.id,
categories.Category,
COUNT(*) AS items,
SUM(items.price) as price
FROM
`categories`
JOIN items
ON categories.id = items.category
GROUP BY
categories.id,
categories.Category
I have three tables and they are the following
User Table
+---------+-----------+--------+
| user_id | user_name | branch |
+---------+-----------+--------+
| 1 | John | 1 |
| 2 | Jim | 2 |
| 3 | Jern | 3 |
| 4 | Jack | 1 |
| 5 | Jery | 2 |
| 6 | Tom | 3 |
| 7 | Sona | 1 |
| 8 | Tina | 3 |
+---------+-----------+--------+
Branch Table
+-----------+----------------+
| branch_id | branch_name |
+-----------+----------------+
| 1 | IT |
| 2 | SALES |
| 3 | Administration |
+-----------+----------------+
Enquiry Table
+------------+---------------+---------+
| enquiry_id | enquiry_name | user_id |
+------------+---------------+---------+
| 1 | enqury_test1 | 1 |
| 2 | enqury_test2 | 2 |
| 3 | enqury_test3 | 1 |
| 4 | enqury_test4 | 3 |
| 5 | enqury_test5 | 2 |
| 6 | enqury_test6 | 5 |
| 7 | enqury_test7 | 1 |
| 8 | enqury_test8 | 2 |
| 9 | enqury_test9 | 4 |
| 10 | enqury_test10 | 6 |
| 11 | enqury_test11 | 2 |
| 12 | enqury_test12 | 7 |
+------------+---------------+---------+
From the above tables its clear that, each branch contains a number of users.
These users post multiple enquiries.
I need to get the total number of enquiries in each branch as
branch id => number of enquiries
I have tried various queries. But i couldn't get the result. Any one can help?
I am using MySQL and i need a single query to do this.
Thanks in advance
You need count and group by
select
b.branch_id,
count(e.user_id) as `total_enq`
from Branch b
left join User u on u.branch = b.branch_id
left join Enquiry e on e.user_id = u.user_id
group by b.branch_id
The query you have to perform to get you desired result is like this :-
$query = "SELECT u.branch, COUNT(u.user_id) AS `total_enquires`
FROM enquiry e INNER JOIN user u ON e.user_id = u.user_id
GROUP BY u.branch"
This will help you,and i think you don't need to join branch table as user table already contain branch_id.
This is the query
SELECT `branch`,`branch_name`,count(`user`.`user_id`),count(`enquiry_id`) FROM `user` inner join `branch` on `user`.`branch`=`branch`.`branch_id` inner join `enquiry` on `user`.`user_id`=`enquiry`.`user_id` group by `branch`
try it here
http://sqlfiddle.com/#!9/cf3eb/1
SELECT
bt.branch_id
,COUNT(enquiry_id) AS total_enquiry
FROM
enquiry_table et
INNER JOIN user_table ut on ut.user_id = et.user_id
INNER JOIN branch_table bt ON bt.branch_id = ut.branch
WHERE
1=1
GROUP BY
bt.branch_id
you can try this
I have three tables,
Product_to_categories => contains two columns (Category_id, Product_id)
Product => contains a few columns (product_id, name, sku, ...)
categories => contains a few columns (category_id, name, ....)
I would like to get a result set for the first 10 unique category_id's joined with the product table, a category can have many product assigned to it, i want all products within one category returned, but i'd like to only get the products from the first 8 categories...
Current query:
SELECT p2c.category_id, p.pname, c.category_name
FROM product p LEFT JOIN product_to_category p2c
ON (p.product_id = p2c.product_id)
Left Join category c (p2c.category_id = c.category) LIMIT 0,8
Current output
catID | p.name | catName |<br/>
1 | docs | shoe<br/>
1 | bob | shoe<br/>
1 | mom | shoe<br/>
1 | cat | shoe<br/>
1 | dang | shoe<br/>
1 | kit | shoe<br/>
2 | pis | book<br/>
2 | jiz | book<br/>
Currently i only get the first 8 results regardsless, however i am looking to get the following output:
catID | p.name | catName |<br/>
1 | docs | shoe<br/>
1 | bob | shoe<br/>
1 | mom | shoe<br/>
1 | cat | shoe<br/>
1 | dang | shoe<br/>
1 | kit | shoe<br/>
2 | pis | book<br/>
2 | jiz | book<br/>
3 | docs | shirt<br/>
3 | bob | shirt<br/>
3 | mom | shirt<br/>
4 | cat | light<br/>
4 | dang | light<br/>
5 | kit | sound<br/>
6 | pis | mic<br/>
6 | jiz | mic<br/>
7 | docs | pen<br/>
7 | bob | pen<br/>
7 | mom | pen<br/>
7 | cat | pen<br/>
8 | dang | lace<br/>
8 | kit | lace<br/>
8 | pis | lace<br/>
8 | jiz | lace<br/>
i would like the resultset to contain results of all products that are assigned to the first 8 categories...
Please advise.
Thanks
Hadi
I'm a bit unsure why your query is starting from the product table, so I'm not going to - but aside from this, I think what you need is:
SELECT c.category_id, p.pname, c.category_name
FROM category c
INNER JOIN product_to_category pc ON c.category_id = pc.category_id
INNER JOIN product p ON pc.product_id = p.product_id
WHERE c.category_id in (SELECT TOP 8 category_id FROM Category)
ORDER BY 1, 2
I have a table customers
| id | firstname |
| 1 | paul |
| 2 | steve |
| 3 | chris |
second table called list_customer
| id | id_customer | id_list |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
Each customer can be in x number of lists
third table called list
| id_list | color |
| 1 | #fff |
| 2 | #000 |
| 3 | #ccc |
With mysql query I want to get firstname and list colors.
Customers can be in multiple numbers of lists.
select c.firstname, group_concat(l.color) as colors
from customers c
inner join list_customer lc on lc.id_customer = c.id
inner join list l on l.id_list = lc.id_list
group by c.firstname
Try this:
SELECT c.FIRSTNAME,
l.COLOR AS ListColors
FROM CUSTOMERS c,
LIST_CUSTOMER lc,
LIST l
WHERE lc.ID_CUSTOMER = c.ID
AND l.ID_LIST = lc.ID_LIST
GROUP BY c.FIRSTNAME
I have a problem with a SQL query and the resultset being returned not being what I expected.
I have these three tables that I am trying to relate.
events_detail
__________________
| ID | start_date |
| 1 | 2012-08-09 |
| 2 | 2013-02-13 |
| 3 | 2012-12-12 |
| 4 | 2013-01-21 |
| 5 | 2012-12-25 |
-------------------
where ID is the primary key
events_category_relationship
__________________________
| ID | event_id | cat_id |
| 1 | 1 | 1 |
| 2 | 2 | 4 |
| 3 | 3 | 2 |
| 4 | 4 | 2 |
| 5 | 5 | 3 |
--------------------------
where ID is primary key
events_category_detail
__________________________________
| ID | name | description |
| 1 | Europe | Kings and castles! |
| 2 | USA | Freedoms |
| 3 | China | Made in China |
| 4 | UK | Big Brother |
------------------------------------
where ID is primary key
What I need to do is grab only 1 event from each category and sorted by date of earliest appearance. So what I should expect in my result is the following
Result Set
________________________________________________________________
| e_id | start_date | c_id | category_name | category_desc |
| 1 | 2012-08-09 | 1 | Europe | Kings and castles! |
| 3 | 2012-12-12 | 2 | USA | Freedoms |
| 5 | 2012-12-25 | 3 | China | Made in China |
| 2 | 2013-02-13 | 4 | UK | Big Brother |
-----------------------------------------------------------------
My SQL query that I tried looks like this
SELECT e.id, e.start_date, c.category_name, c.category_desc
FROM events_detail e
JOIN events_category_relationship r ON r.event_id = e.id
JOIN events_category_detail c ON c.id = r.cat_id
ORDER BY date(e.start_date)
This just joins the 3 tables and returns the result in order by date. What I am stuck on is making it so that only one of each category is displayed like the desired result set above. I have tried using DISTINCT c.category_name and GROUP BY c.category_name, but none of them works.
Any help or advice will be greatly appreciated.
You will want to use a subquery to get the min(start_date) for each name and description. You will then use this result to join back to your events_details table to get the id associated with the data in the subquery:
SELECT e.id,
d.start_date,
d.name,
d.description
FROM events_detail e
INNER JOIN
(
select min(e.start_date) Start_date,
c.name,
c.description
from events_detail e
INNER JOIN events_category_relationship r
ON r.event_id = e.id
INNER JOIN events_category_detail c
ON c.id = r.cat_id
group by c.name, c.description
) d
on e.start_date = d.Start_date
ORDER BY date(e.start_date)
See SQL Fiddle with Demo