Get multiple values from table - php

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

Related

mysql Query to avoid duplicate results

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

MySQL getting total number of enquiries for each branch

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

In MySQL I need to retrieve artist with highest points by location

In MySQL I need to get artist with highest points by location. Please help.
user_location_tbl (user location table)
|--------------------------------------|
| countryLat | countryLong | userid |
|--------------------------------------|
| 31.695766 | 54.624023 | 1 |
| 20.593684 | 78.96288 | 2 |
| 20.593684 | 78.96288 | 3 |
| 20.593684 | 78.96288 | 4 |
| 31.695766 | 54.624023 | 5 |
|--------------------------------------|
fans_table (to view which artist has which user as a fan)
|----------------------|----------
| artist_id | user_id | Points |
|----------------------|----------
| 1 | 1 | 20 |
| 1 | 2 | 30 |
| 2 | 1 | 40 |
| 2 | 3 | 40 |
| 3 | 1 | 60 |
|----------------------|----------
artist_table (list of artist)
|-------------------|
| artistid | name |
|-------------------|
| 1 | raja |
| 2 | sekar |
| 3 | thomas |
|-------------------|
I need to get location wise which artist is having highest point but I can't do it in a single query. If I put sum(points) and group by countryLat, countryLong, artistid, I get the following result...
|---------------------------------|
| 100 | 20.593684 | 78.96288 | 1 |
| 50 | 20.593684 | 78.96288 | 2 |
| 100 | 31.695766 | 54.624023 | 3 |
| 90 | 31.695766 | 54.624023 | 1 |
|---------------------------------|
...but I only need the artist with highest point on that location as in the example below...
|---------------------------------|
| 100 | 20.593684 | 78.96288 | 1 |
| 100 | 31.695766 | 54.624023 | 3 |
|---------------------------------|
SQL
SELECT Sum(c.tot_points),
a.artist_id
FROM `fans_table` AS a
INNER JOIN `user_location_tbl` AS b
ON a.user_id = b.user_id
INNER JOIN artist_table AS c
ON a.artist_id = c.id
GROUP BY b.countrylat,
b.countrylong,
a.artist_id
Above is my query
So assuming I understand your question right you want to get which artist has the most points for each location so this would be the process to figure it out manually:
// First we add up all the points for each artist in each location:
|----------------------------------------------|
| Points | countryLat | countryLong | artistid |
|----------------------------------------------|
| 30 | 20.593684 | 78.96288 | 1 |
| 40 | 20.593684 | 78.96288 | 2 |
| 0 | 20.593684 | 78.96288 | 3 |
| 20 | 31.695766 | 54.624023 | 1 |
| 40 | 31.695766 | 54.624023 | 2 |
| 60 | 31.695766 | 54.624023 | 3 |
|----------------------------------------------|
// Then we get the max for each location
|----------------------------------------------|
| Points | countryLat | countryLong | artistid |
|----------------------------------------------|
| 40 | 20.593684 | 78.96288 | 2 |
| 60 | 31.695766 | 54.624023 | 3 |
|----------------------------------------------|
The query to do this is as follows
SELECT artist_id, ul.countryLat, ul.countryLong, max_points
FROM fans_table f
JOIN user_location_tbl ul
ON ul.userid = f.user_id
JOIN (
SELECT countryLat, countryLong, MAX(f.total_points) max_points
FROM
(SELECT artist_id, countryLat, countryLong, SUM(Points) as total_points
FROM fans_table f
JOIN user_location_tbl ul
ON ul.userid = f.user_id
GROUP BY artist_id, countryLat, countryLong) f
GROUP BY countryLat, countryLong) p
ON p.countryLat = ul.countryLat
AND p.countryLong = ul.countryLong
GROUP BY f.artist_id, ul.countryLat, ul.countryLong
HAVING p.max_points = SUM(Points)
Making an assumption that the points in an area will be unique (ie, either there won't be 2 equally popular artists in an area, or if there are you want them both) then something like the following will do it
SELECT Sub3.artist_id, Sub3.countryLat, Sub3.countryLong, Sub2.MaxArtistLocalPoints
FROM
(
SELECT a.artist_id, countryLat, countryLong, SUM(a.Points) AS ArtistLocalPoints
FROM `fans_table` AS a
INNER JOIN `user_location_tbl` AS b ON a.user_id=b.user_id
INNER JOIN artist_table AS c ON a.artist_id=c.id
GROUP BY a.artist_id, countryLat, countryLong
) Sub3
INNER JOIN
(
SELECT countryLat, countryLong, MAX(ArtistLocalPoints) AS MaxArtistLocalPoints
FROM
(
SELECT a.artist_id, countryLat, countryLong, SUM(a.Points) AS ArtistLocalPoints
FROM `fans_table` AS a
INNER JOIN `user_location_tbl` AS b ON a.user_id=b.user_id
INNER JOIN artist_table AS c ON a.artist_id=c.id
GROUP BY a.artist_id, countryLat, countryLong
) Sub1
) Sub2
ON Sub3.countryLat = Sub2.countryLat
AND Sub3.countryLong = Sub2.countryLong
AND Sub3.ArtistLocalPoints = Sub2.MaxArtistLocalPoints

SQL query statement table joins

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

Row count MySQL with three tables

My MySQL DB looks like this
**table_schools**
id | name
1 | school_1
2 | school_2
**table_classes**
id | class | school_id
1 | a | 1
2 | b | 1
3 | c | 2
4 | d | 2
5 | e | 2
**table_students**
id | name | class_id
1 | Nick | 1
2 | Tom | 2
3 | Kevin | 3
4 | Jane | 4
5 | Mark | 5
6 | Tim | 5
7 | Lynn | 5
I would like to have an output like this:
school_name | class_count | student_count
school_1 | 2 | 2
school_2 | 3 | 5
Is there a way to do this in ONE sql query? And how?
SELECT s.name, COUNT(DISTINCT c.id) AS classes, COUNT(st.id) AS students
FROM table_schools s
LEFT JOIN
table_classes c
ON c.school_id = s.id
LEFT JOIN
table_students st
ON st.class_id = c.id
GROUP BY
s.id
SELECT table_schools.name, COUNT(table_classes.name) AS classes, COUNT(table_students.id) AS students
FROM table_schools
LEFT JOIN table_classes ON table_schools.id = table_classes.school_id
LEFT JOIN table_students ON table_students.class_id = table_classes.id
GROUP BY table_schools.id, table_classes.id
ORDER BY table_schools.name

Categories