I have a glitch which i cannot solve,let me elaborate...
These are my MySQL tables...
Therapists table
id therapist_name
1 Therapist 1
2 Therapist 2
Location table
+-----+------------+--+
| id | name | |
+-----+------------+--+
| 1 | Location 1 | |
| 2 | Location 2 | |
| 3 | Location 3 | |
+-----+------------+--+
Days_location table
+-----+-----------+--------------+-------------+--+
| id | day | therapist_id | location_id | |
+-----+-----------+--------------+-------------+--+
| 1 | monday | 1 | 1 | |
| 2 | monday | 1 | 2 | |
| 3 | wednesday | 1 | 3 | |
| 4 | wednesday | 2 | 1 | |
| 5 | tuesday | 2 | 2 | |
| 6 | friday | 2 | 1 | |
| 7 | friday | 2 | 2 | |
| 8 | friday | 1 | 1 | |
+-----+-----------+--------------+-------------+--+
Now i want to get every therapist with locations for every day,for example something like this:
therapist_name=>Therapist 1,day_locations=>monday(Location1,Location2),friday(Location1)
I need it to be as a select variable,this was my query but i got stuck there:
SELECT t.*,GROUP_CONCAT(
SELECT CONCAT(dl2.day,GROUP_CONCAT(dl2.location_id)) as concated
FROM days_location dl2
WHERE therapist_id=85
GROUP BY dl2.day
) as day_location
FROM therapists t
LEFT JOIN days_location dl
ON dl.therapist_id=t.id
This of course doesn't work,what am i doing wrong...should i try a different approach or make my tables different?
I believe this is what you're looking for, or could get you started:
SELECT
t.therapist_name,
dl.day,
GROUP_CONCAT(DISTINCT dl.name SEPARATOR ',') AS locations
FROM
therapists t
LEFT JOIN days_location dl ON dl.therapist_id = t.id
LEFT JOIN location l ON dl.location_id = l.id
GROUP BY t.therapist_name, dl.day
For therapists.id = 1 this should give you results:
+----------------+-----------+-----------------------+
| therapist_name | day | locations |
+----------------+-----------+-----------------------+
| Therapist 1 | monday | Location 1,Location 2 |
| Therapist 1 | wednesday | Location 3 |
| Therapist 1 | friday | Location 1 |
+----------------+-----------+-----------------------+
If you need to concatenate day with locations column then use a simple CONCAT():
SELECT
therapist_name,
CONCAT(day, '(', locations, ')') AS locations
FROM (
SELECT
t.therapist_name,
dl.day,
GROUP_CONCAT(DISTINCT dl.name SEPARATOR ',') AS locations
FROM
therapists t
LEFT JOIN days_location dl ON dl.therapist_id = t.id
LEFT JOIN location l ON dl.location_id = l.id
GROUP BY t.therapist_name, dl.day
) t
GROUP BY therapist_name, locations
Output should look like:
+----------------+-------------------------------+
| therapist_name | locations |
+----------------+-------------------------------+
| Therapist 1 | monday(Location 1,Location 2) |
| Therapist 1 | wednesday(Location 3) |
| Therapist 1 | friday(Location 1) |
+----------------+-------------------------------+
If you need to group it all into one row for each therapist, then you could GROUP_CONCAT() again.
Edit after comments:
SELECT
therapist_name,
GROUP_CONCAT( CONCAT(day, '(', locations, ')') SEPARATOR ',' ) AS locations
FROM (
SELECT
t.therapist_name,
dl.day,
GROUP_CONCAT(DISTINCT dl.name SEPARATOR ',') AS locations
FROM
therapists t
LEFT JOIN days_location dl ON dl.therapist_id = t.id
LEFT JOIN location l ON dl.location_id = l.id
GROUP BY t.therapist_name, dl.day
) t
GROUP BY therapist_name
I haven't tested the code so there may be some minor mistakes to tweak. No way of testing it atm.
Related
I have a working query using INNER JOIN and a subquery but was wondering if there is a more effient way of writing it.
with prl
as
(
SELECT `number`, creator, notes
FROM ratings
INNER JOIN
projects on ratings.project_id = projects.project_id
WHERE ratings.rating = 5 AND projects.active = 1
)
SELECT prl.`number`, creator, notes
FROM prl
INNER JOIN(
SELECT `number`
HAVING COUNT(creator) > 1
)temp ON prl.`number` = temp.`number`
ORDER BY temp.`number`
projects table
project_id| number | creator | active |
| 1 | 3 | bob | 1 |
| 2 | 4 | mary | 1 |
| 3 | 5 | asi | 1 |
rating table
project_id| notes | rating |
| 1 | note1 | 5 |
| 1 | note2 | 5 |
| 3 | note3 | 5 |
| 1 | note4 | 1 |
| 2 | note5 | 5 |
| 3 | note6 | 2 |
result
| number | creator | notes |
| 3 | bob | note1 |
| 3 | bob | note2 |
It seems like you're using MySQL version that support window function. If so, then try this:
SELECT number, creator, notes
FROM
(SELECT p.number, p.creator, r.notes,
COUNT(creator) OVER (PARTITION BY creator) AS cnt
FROM project p
JOIN rating r ON p.project_id=r.project_id
WHERE r.rating=5
AND p.active = 1) v
WHERE cnt=2;
As far as whether this is more efficient, I'm not really sure because it depends in your table indexes but for a small dataset, I assume this will do well.
Demo fiddle
so what I am trying to do is having 3 tables (pictures, collections, and bridge) with the following columns:
Collections Table:
| id | name |
------------------
| 1 | coll1 |
| 2 | coll2 |
------------------
Pictures Table: (timestamps are unix timestamps)
| id | name | timestamp |
-------------------------
| 5 | Pic5 | 1 |
| 6 | Pic6 | 19 |
| 7 | Pic7 | 3 |
| 8 | Pic8 | 892 |
| 9 | Pic9 | 4 |
-------------------------
Bridge Table:
| id | collection | picture |
-----------------------------
| 1 | 1 | 5 |
| 2 | 1 | 6 |
| 3 | 1 | 7 |
| 4 | 1 | 8 |
| 5 | 2 | 5 |
| 6 | 2 | 9 |
| 7 | 2 | 7 |
-----------------------------
And the result should look like this:
| collection_name | picture_count | newest_picture |
----------------------------------------------------
| coll1 | 4 | 8 |
| coll2 | 3 | 9 |
----------------------------------------------------
newest_picture should always be the picture with the heighest timestamp in that collection and I also want to sort the result by it. picture_count is obviously the count of picture in that collection.
Can this be done in a single statement with table joins and if yes:
how can I do this the best way?
A simple method uses correlated subqueries:
select c.*,
(select count(*)
from bridge b
where b.collection = c.id
) as pic_count,
(select p.id
from bridge b join
pictures p
on b.picture = b.id
where b.collection = c.id
order by p.timestamp desc
limit 1
) as most_recent_picture
from collections c;
A more common approach would use window functions:
select c.id, c.name, count(bp.collection), bp.most_recent_picture
from collections c left join
(select b.*,
first_value(p.id) over (partition by b.collection order by p.timestamp desc) as most_recent_picture
from bridge b join
pictures p
on b.picture = p.id
) bp
on bp.collection = c.id
group by c.id, c.name, bp.most_recent_picture;
+----------+--------+
| name | version|
+----------+--------+
| book | 2 |
| book | 1 |
| book | 1 |
| pen | 1 |
| pen | 2 |
| pen | 2 |
| pen | 2 |
| paper | 1 |
+----------+--------+
I have the table above and i want to make a query to group by name and count by version(row)
Result:
+----------+--------+--------+
| name | version| count |
+----------+--------+--------+
| book | 1 | 2 |
| book | 2 | 1 |
| pen | 1 | 1 |
| pen | 2 | 3 |
| paper | 1 | 1 |
| paper | 2 | 0 |
+----------+--------+--------+
The query would be
SELECT name, version, count(*) as count
FROM your_table_name
GROUP BY name, version
If you want all name/version combinations, then use a cross join to generate all rows and then left join to bring in the existing data:
select n.name, v.version, count(t.name)
from (select distinct name from t) n cross join
(select distinct version from t) v left join
t
on t.name = n.name and t.version = v.version
group by n.name, v.version
order by n.name, v.version;
I have three tables which I successfully joined with the following sql query
SELECT `bonuses`.`id`, `bonuses`.`bonus_name`, `bonuses`.`size`, creatorName.`name`, GROUP_CONCAT(DISTINCT bonus_user.user_id ORDER BY bonus_user.user_id SEPARATOR ', ') as bonusUsers from `bonuses`
inner join `users` as creatorName on `bonuses`.`created_from` = creatorName.`id`
inner join `bonus_user` on `bonuses`.`id` = `bonus_user`.`bonus_id`
group by `bonuses`.`id`
The result I get is as following. As a further step I want to replace the ids in the column "bonusUsers" by the names from the users table. How do I manage this?
+----+--------------+------+--------------+--------------+
| id | bonus_name | size | name | bonusUsers |
+----+--------------+------+--------------+--------------+
| 3 | Bonus Test 3 | 5 | Test1 | 1, 2, 3 |
| 4 | Bonus Test 4 | 3 | Test1 | 1, 2, 3 |
+----+--------------+------+--------------+--------------+
users
+----+-------+
| id | name |
+----+-------+
| 1 | Test1 |
| 2 | Test2 |
| 3 | Test3 |
+----+-------+
bonuses
+----+--------------+------+--------------+
| id | bonus_name | size | created_from |
+----+--------------+------+--------------+
| 1 | Bonus Test 1 | 1 | 1 |
| 2 | Bonus Test 2 | 1 | 1 |
| 3 | Bonus Test 3 | 5 | 1 |
| 4 | Bonus Test 4 | 3 | 1 |
+----+--------------+------+--------------+
bonus_user
+----+----------+------------+
| id | bonus_id | bonus_user |
+----+----------+------------+
| 1 | 3 | 1 |
| 2 | 3 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 1 |
| 5 | 4 | 2 |
| 6 | 4 | 3 |
+----+----------+------------+
Do another join with users table
SELECT `b`.`id`, `b`.`bonus_name`, `b`.`size`, u.`name`,
GROUP_CONCAT(DISTINCT bu1.`name` ORDER BY bu.user_id SEPARATOR ', ') as bonusUsers
from `bonuses` b
inner join `users` as u on `b`.`created_from` = u.`id`
inner join `bonus_user` bu on `b`.`id` = `bu`.`bonus_id`
inner join `users` as bu1 on `bu`.`user_id` = bu1.`id`
group by `b`.`id`, `b`.`bonus_name`, `b`.`size`, u.`name`
Demo
You need to aliase the users table and join it to the bonus_user tables to get the name of the user from the aliased table
SELECT `bonuses`.`id`, `bonuses`.`bonus_name`, `bonuses`.`size`, creatorName.`name`,
GROUP_CONCAT(DISTINCT user_names.name ORDER BY bonus_user.user_id SEPARATOR ', ') as bonusUsers from `bonuses`
inner join `users` as creatorName on `bonuses`.`created_from` = creatorName.`id`
inner join `bonus_user` on `bonuses`.`id` = `bonus_user`.`bonus_id`
inner join `users AS user_names` on `bonus_user`.`bonus_user` = `user_names`.`id`
group by `bonuses`.`id`
This may help as well https://www.w3schools.com/sql/sql_alias.asp
Well, I have three different tables (check below). I wanted to make that I can see, which is the latest image (req_image_1, etc) saved for each category, which is not parent (cat_parent = 0)
One table, which holds general information about requests
+----+--------------------------+------------+
| id | req_name | req_parent |
+----+--------------------------+------------+
| 3 | Send pack | 19 |
| 4 | Go Visit | 18 |
| 5 | Stop by | 19 |
| 6 | Deliver cookies | 34 |
+----+--------------------------+------------+
Second table, which holds meta information about requests
+----------+------------+------------+----------------------------+
| umeta_id | request_id | meta_key | meta_value |
+----------+------------+------------+----------------------------+
| 1 | 3 | req_city | London |
| 2 | 3 | req_street | 11 Baker street |
| 3 | 3 | req_img_1 | a1c8f69edb37bf6c6.jpg |
| 4 | 4 | req_city | Manchester |
| 5 | 4 | req_street | 71 Main street |
| 6 | 4 | req_img_2 | a71f4160d7f7f7555.jpg |
| 7 | 5 | req_city | Sheffield |
| 8 | 5 | req_street | 240 Duke street |
| 9 | 6 | req_city | Manchester |
| 10 | 6 | req_street | 13 Chapel street |
| 11 | 6 | req_img_1 | 854b9faaa53d8fe02.jpg |
+----------+------------+------------+----------------------------+
Third table, which holds information about categories
+----+------------------------+------------+
| ID | cat_name | cat_parent |
+----+------------------------+------------+
| 1 | Category_01 | 0 |
| 6 | Category_02 | 0 |
| 18 | Category_01_01 | 1 |
| 19 | Category_01_02 | 1 |
| 34 | Category_02_01 | 6 |
+----+------------------------+------------+
So far I managed, that I could get all images for each category with this query:
SELECT cat.cat_parent AS category, req.ID, meta.meta_value AS image
FROM d_requests req
LEFT JOIN d_requests_meta meta ON ( req.ID = meta.request_id )
LEFT JOIN d_categories cat ON ( req.req_parent = cat.ID )
WHERE meta.meta_key LIKE 'req_img_%'
I got this result:
+------------+----+-----------------------+
| category | ID | image |
+------------+----+-----------------------+
| 1 | 3 | a1c8f69edb37bf6c6.jpg |
| 1 | 4 | a71f4160d7f7f7555.jpg |
| 6 | 6 | 854b9faaa53d8fe02.jpg |
+------------+----+-----------------------+
But I wanted to make enhancement, so I would get only result, where each category has only one image against, for example category 1, has image a71f4160d7f7f7555.jpg and category 6 has image 854b9faaa53d8fe02.jpg
I bet, that I miss some basic knowledge, and simple enhancement with subquery and selecting MAX would work as a charm.
Thanks!
SQL Fiddle
select
category,
(select request_id
from d_requests_meta
where umeta_id = s.ID
) as ID,
(select meta_value
from d_requests_meta
where umeta_id = s.ID
) AS image
from (
SELECT cat.cat_parent AS category, max(meta.umeta_id) ID
FROM d_requests req
LEFT JOIN d_requests_meta meta ON ( req.ID = meta.request_id )
LEFT JOIN d_categories cat ON ( req.req_parent = cat.ID )
WHERE meta.meta_key LIKE 'req_img_%'
group by cat.cat_parent
) s
SELECT category, ID, image
FROM ( SELECT cat.cat_parent AS category, req.ID, meta.meta_value AS image
FROM d_requests AS req
LEFT JOIN d_requests_meta AS meta
ON req.ID = meta.request_id
LEFT JOIN d_categories AS cat
ON req.req_parent = cat.ID
WHERE meta.meta_key LIKE 'req_img_%'
ORDER BY req.ID DESC) AS h
GROUP BY category
I edited Clodoaldo's answer with the use of the inofficial MySQL assumption that GROUP BY will return the 1st row based on ORDER BY in subquery.
Try this http://sqlfiddle.com/#!2/bfe9a/19
SELECT Category, ID, Image FROM (
SELECT Category, ID, Image,
#id:=CASE WHEN #category <> category THEN 1 ELSE #id+1 END AS ImgRank,
#category:=category AS categoryTemp FROM
(SELECT #id:= 0) AS i,
(SELECT #category:= 0) AS c,
(
SELECT cat.cat_parent AS category, req.ID, meta.meta_value AS image
FROM d_requests req
LEFT JOIN d_requests_meta meta ON ( req.ID = meta.request_id )
LEFT JOIN d_categories cat ON ( req.req_parent = cat.ID )
WHERE meta.meta_key LIKE 'req_img_%'
ORDER BY cat.cat_parent, req.id desc
) Vw
) vw2 WHERE IMGRANK = 1