I want to find all ids of which group_concat only contains 'a'.
Here is simplified table from mine.
╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║ 1 ║ a,b,b ║
║ 2 ║ a ║
║ 3 ║ a,a ║
║ 4 ║ a,a,a ║
║ 5 ║ a,b,a ║
╚════╩══════════════╩
And the table below is what I want to achieve.
╔════╦══════════════╦
║ id ║ group_concat ║
╠════╬══════════════╬
║ 2 ║ a ║
║ 3 ║ a,a ║
║ 4 ║ a,a,a ║
╚════╩══════════════╩
And this is the query statement I am trying to use.
select id, group_concat(val)
from user
group by id
having group_concat(val) = 'a'
Thanks in advance
Try this:
select id, group_concat(val)
from user
group by id
having count(distinct val) = 1 and max(val) = 'a'
Related
I am learning sql query.
Here is my table simplified
╔════╦══════════════╦══════════════╦════════╦
║ id ║ user_id ║ department_id║ salary ║
╠════╬══════════════╬══════════════╬════════╬
║ 1 ║ 1 ║ 3 ║ 100 ║
║ 2 ║ 2 ║ 3 ║ 50 ║
║ 3 ║ 1 ║ 3 ║ 30 ║
║ 4 ║ 2 ║ 3 ║ 20 ║
║ 5 ║ 2 ║ 3 ║ 20 ║
╚════╩══════════════╩══════════════╩════════╩
Here is what I want to have below
╦══════════════╦══════════════╦════════╦
║ user_id ║ department_id║ salary ║
╠══════════════╬══════════════╬════════╬
║ 1 ║ 3 ║ 130 ║
║ 2 ║ 3 ║ 90 ║
╚══════════════╩══════════════╩════════╩
I want to add user salary if they are in the same department.
I have nooo ideas how to start.
Does anyone have good feedback that I can start with?
Thank you in advance
Try this query:
Select user_id, department_id, sum(salary) from table
Group by user_id, department_id
In the select clause, you have the columns you wish to select and your group by clause contains the grouping.
The tables below are an example table structure.
The query I am trying to write is something like:
SELECT * FROM jobs LEFT JOIN assigned ON jobs.id = assigned.job_id;
However, as you can see the left join will product multiple matches in the case of job 100, but the above query will only return one match.
Is it possible to concatenate the results into a comma separated string for the left join?
If possible, a step further would be, is it possible to replace the assigned.user_id with the users.name column in the concatenated comma separated string.
users
╔═══╦════════════╦═════════════╗
║ ║ id ║ name ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 1 ║ Matt ║
║ 2 ║ 2 ║ Phil ║
║ 3 ║ 3 ║ Chris ║
╚═══╩════════════╩═════════════╝
jobs
╔═══╦════════════╦═════════════╗
║ ║ id ║ name ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 100 ║ Do this ║
║ 2 ║ 101 ║ Do that ║
║ 3 ║ 102 ║ And this ║
╚═══╩════════════╩═════════════╝
assigned
╔═══╦════════════╦═════════════╗
║ ║ user_id ║ job_id ║
╠═══╬════════════╬═════════════╣
║ 1 ║ 1 ║ 100 ║
║ 2 ║ 2 ║ 100 ║
║ 3 ║ 1 ║ 101 ║
╚═══╩════════════╩═════════════╝
SELECT jobs.id,
jobs.name,
GROUP_CONCAT( users.name order by users.name) as workersOnTheJob
FROM
jobs
LEFT JOIN assigned
ON jobs.id = assigned.job_id
LEFT JOIN users
on assigned.user_id = users.id
group by
jobs.id,
jobs.name
I have two columns in a table say, LIKE and FAVORITES (int value)
See the chart:
╔════╦══════╦══════════╗
║ ID ║ LIKE ║ FAVORITE ║
╠════╬══════╬══════════╣
║ 1 ║ 25 ║ 9 ║
║ 2 ║ 5 ║ 17 ║
║ 3 ║ 6 ║ 1 ║
║ 4 ║ 45 ║ 0 ║
║ 5 ║ 3 ║ 44 ║
╚════╩══════╩══════════╝
Now, I want to select the Maximum Like and Favorites IDs from the SELECT clause.
I have tried
SELECT ID from TABLE WHERE CONDITION ORDER BY LIKE,FAVORITES DESC
But the result shows the rows based on LIKE DESC order.
The result should be
╔════╗
║ ID ║
╠════╣
║ 5 ║
║ 4 ║
║ 1 ║
║ 2 ║
║ 3 ║
╚════╝
I think you need to add those two columns. eg,
SELECT ID
FROM tableName
ORDER BY `LIKE` + FAVORITE DESC
SQLFiddle Demo
Result:
╔════╗
║ ID ║
╠════╣
║ 5 ║
║ 4 ║
║ 1 ║
║ 2 ║
║ 3 ║
╚════╝
I ran into some problems while structuring my database, and I will ask two questions.
First question: below table needs to be merged by the same IDs
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
║ 0 ║ ║ 11 ║ ║
║ 0 ║ ║ ║ 6 ║
║ 1 ║ Dave ║ ║ ║
║ 1 ║ ║ 12 ║ ║
║ 1 ║ ║ ║ 7 ║
╚═════╩═══════╩═════╩═══════╝
so it should look like this;
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ 11 ║ 6 ║
║ 0 ║ Dave ║ 12 ║ 7 ║
╚═════╩═══════╩═════╩═══════╝
NOTE: id* is not AUTO_INCREMENT
Second question: You probably think that the former database structure is poor. The good thing is, I haven't created the database yet and I have been looking for a solution to add data to an existing row without removing old information, but if there is no old information, it would create a new row. Thanks in advance.
Second question explained
Virgin table
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ ║ ║ ║ ║
╚═════╩═══════╩═════╩═══════╝
some SQL statement
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
╚═════╩═══════╩═════╩═══════╝
the same SQL statement with different parameters
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
║ 1 ║ Dave ║ ║ ║
╚═════╩═══════╩═════╩═══════╝
another SQL statement
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ ║
║ 1 ║ Dave ║ 12 ║ ║
╚═════╩═══════╩═════╩═══════╝
another SQL statement
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 0 ║ John ║ ║ 6 ║
║ 1 ║ Dave ║ 12 ║ ║
╚═════╩═══════╩═════╩═══════╝
... and so on.
You should be able to apply an aggregate function to all the columns and then GROUP BY id:
select id,
max(name) name,
max(age) age,
max(grade) grade
from yourtable
group by id
See SQL Fiddle with Demo
As far as the DB structure, the only issue that I see is that you are inserting multiple records for the same user. You should be using an UPDATE statement to use the values instead of inserting.
It sounds like you want to use the REPLACE function in MySQL (here is a tutorial).
So the query would be similar to this:
REPLACE
INTO yourtable (`id`, `name`, `age`, `grade`)
VALUES (0, 'john', 11, null);
See SQL Fiddle with Demo
You could group by id, and use any aggregate function to pick the non-null row. This example uses max, but min would work too:
select id
, max(name) as name
, max(age) as age
, max(grade) as grade
from YourTable
group by
id
Hi i have following table on mysql DB.
╔═══════════╦═════════╦════════╦════════════════╗
║ REVIEW_ID ║ USER_ID ║ STATUS ║ DATE_ADDED ║
╠═══════════╬═════════╬════════╬════════════════╣
║ 218 ║ 2 ║ cool ║ 20130121134811 ║
║ 218 ║ 2 ║ cool ║ 20130121134812 ║
║ 218 ║ 2 ║ lame ║ 20130121134813 ║
║ 218 ║ 2 ║ funny ║ 20130121134814 ║
║ 218 ║ 2 ║ funny ║ 20130121134815 ║
║ 218 ║ 2 ║ funny ║ 20130121134816 ║
║ 218 ║ 2 ║ lame ║ 20130121134817 ║
╚═══════════╩═════════╩════════╩════════════════╝
how can i get a result where when i do query based on user_id i need to get result of total status for each type:
╔════════╦════════════╗
║ STATUS ║ TOTALCOUNT ║
╠════════╬════════════╣
║ cool ║ 2 ║
║ funny ║ 3 ║
║ lame ║ 2 ║
╚════════╩════════════╝
Thanks
Use COUNT() which is an aggregate function, and group them according to their status
SELECT status, COUNT(*) totalCount
FROM tableName
GROUP BY status
SQLFiddle Demo
OTHER(s)
MySQL GROUP BY with some Aggregate Functions List
SELECT status, count(*)
FROM your_table
GROUP BY status
SELECT status, count(*)
FROM yourtable
GROUP BY status
;
SELECT STATUS, COUNT(*) AS TOTALCOUNT
FROM tableName
GROUP BY STATUS
HAVING USER_ID = user_id you need