How to select from select where group by in MySQL? - mysql

I am trying to select and group by column, but keeping other column with all rows. I get error message that #1242 - Subquery returns more than 1 row.
My table
The result I want
Bellow is my query :
SELECT name FROM table WHERE (SELECT pro_id FROM table GROUP BY pro_id)

Try this query:
SELECT Pro_id
, GROUP_CONCAT(Name SEPARATOR ', ') AS Name
FROM MyTable
GROUP BY Pro_ID;
Result:
| PRO_ID | NAME |
-------------------------
| 1 | john, sandra |
| 2 | jeo |
| 3 | bruno, piter |
See this SQLFiddle

You cannot "merge cells" in a SQL result set as you do in your example. The best you could do is either "concat" several values in one "cell" using the GROUP_CONCAT function, or sort the rows to group common values together using ORDER BY clause.
Considering that, you need a very basic SELECT ... ORDER BY statement:
SELECT name, pro_id FROM table ORDER BY pro_id
Producing:
+---------+--------+
| NAME | PRO_ID |
+---------+--------+
| john | 1 |
| sandra | 1 |
| jeo | 2 |
| bruno | 3 |
| piter | 3 |
+---------+--------+
See http://sqlfiddle.com/#!2/6ea716/2

Related

SQL query to find maximum salary

I have a table like this
----------------------
| ID | Name | Salary |
| -- | --- | --- |
| 1 | A | 1000 |
| 2 | B | 4000 |
| 3 | C | 5000 |
| 4 | B | 600 |
| 5 | C | 2000 |
| 6 | A | 5000 |
| 7 | B | 4000 |
----------------------
And I want to query the maximum salary in the whole table and the maximum salary of each student. I can write two queries like,
>> SELECT MAX(Salary) FROM TABLE
>> SELECT NAME, MAX(SALARY) FROM TABLE GROUP BY NAME
Now, I want to do the same in a single query without using two queries. How do I approach?
ROLLUP can be used to give an extra row as a 'summary', and so give the overall MAX value...
SELECT name, MAX(salary)
FROM TABLE
GROUP BY name
WITH ROLLUP
ORDER BY GROUPING(name) DESC,
name
Which would yield...
| Name | Salary |
| --- | --- |
| NULL | 5000 |
| A | 5000 |
| B | 4000 |
| C | 5000 |
here is one way:
SELECT NAME, MAX(SALARY) , max(max(salary)) over()
FROM TABLE GROUP BY NAME
You can use GROUP BY to group around the Name field and ORDER BY ... DESC on the Salary field to get the max.
SELECT PrimaryField, MaxField
FROM MyTable
GROUP By PrimaryField
ORDER BY MaxField DESC;
Explanation:
SELECT PrimaryField, MaxField — The fields we want.
FROM MyTable — The table we want.
GROUP By PrimaryField — What we want to be treated as a primary field.
ORDER BY MaxField DESC; — Since we group by PrimaryField, we get only one row for each PrimaryField unique value. If we order the MaxField, we can get the min or max of it as needed.
For you specifically:
SELECT Name, Salary
FROM TABLE
GROUP By Name
ORDER BY Salary DESC;

MYSQL show all entries sorted by 2 columns random on one column

We are looking to return rows of a query as groups and displaying all entries of the group in the sort order. Randomly based on the set_id... and then in order by the sort_id.
So, randomly it will show:
Carl,
Phil,
Wendy,
Tina,
Rick,
Joe
or
Tina,
Rick,
Joe,
Carl,
Phil,
Wendy
This query is always showing Tina/Rick/Joe first
SELECT * FROM products ORDER BY set_id, rand()
Any help would be appreciated
+---------+--------+-------+----------+
| id | set_id | name | sort_id |
+---------+--------+-------+----------+
| 1 | AA |Rick | 2 |
| 2 | BB |Carl | 1 |
| 3 | AA |Joe | 3 |
| 4 | AA |Tina | 1 |
| 5 | BB |Phil | 2 |
| 6 | BB |Wendy | 3 |
+---------+--------+-------+----------+
if you need a random comma separated name list this will do the trick.
This will keep the groups and the correct sorting within the group.
Query
SELECT
GROUP_CONCAT(Table_names_rand.names) as names
FROM (
SELECT
*
FROM (
SELECT
GROUP_CONCAT(name ORDER BY sort_id) as names
FROM
Table1
GROUP BY
set_id
)
AS Table1_names
ORDER BY
RAND()
)
AS Table_names_rand
Result
| names |
|-------------------------------|
| Carl,Phil,Wendy,Tina,Rick,Joe |
or
| names |
|-------------------------------|
| Tina,Rick,Joe,Carl,Phil,Wendy |
demo http://www.sqlfiddle.com/#!9/487ac9/9
if you need random names as records output.
Query
SELECT
Table1.name
FROM
Table1
CROSS JOIN (
SELECT
GROUP_CONCAT(Table_names_rand.names) as names
FROM (
SELECT
*
FROM (
SELECT
GROUP_CONCAT(name ORDER BY sort_id) as names
FROM
Table1
GROUP BY
set_id
)
AS Table1_names
ORDER BY
RAND()
)
AS Table_names_rand
)
AS Table_names_rand
ORDER BY
FIND_IN_SET(name, Table_names_rand.names)
Result
| name |
|-------|
| Carl |
| Phil |
| Wendy |
| Tina |
| Rick |
| Joe |
or
| name |
|-------|
| Tina |
| Rick |
| Joe |
| Carl |
| Phil |
| Wendy |
demo http://www.sqlfiddle.com/#!9/487ac9/28
If we strip away the randomness of the gorup ordering, your query would look like this:
SELECT
*
FROM
products
ORDER BY
set_id,
sort_id;
The ordering by set_id is necessary to "group" the results, without really grouping them. You do not want to group them, because then the rows with the same group would be aggregated, meaning that only one row per group would be put out.
Since you only want to randomize the groups, you need to write another query that assigns a random number to each group, like the one below:
SELECT
set_id,
RAND() as 'rnd'
FROM
products
GROUP BY
set_id
The GROUP BY clause makes sure, that each group is only selected once. The resultset will look like this:
| set_id | priority |
+--------+---------+
| AA | 0.21 |
| BB | 0.1 |
With that result we can then randomize the output, by combining both queries with a JOIN on the set_id field. This will add the randomly generated number from the second query to the result set of the first query and therefore extend the static set_id with the randomized, but still for all group members equal, rnd:
SELECT
products.*
FROM
products
JOIN (
SELECT
set_id,
RAND() as 'rnd'
FROM
products
GROUP BY
set_id
) as rnd ON rnd.set_id = products.set_id
ORDER BY
rnd.rnd,
products.set_id,
products.sort_id;
Keep in mind, that it is important to still group on products.set_id, because it may be possible that two groups get the same random number assigned. If the result would not be ordered by products.set_id those groups members would then be merged.

SQL : Select statement order by

i want to select a column but with diferent order :
i have 2 table :
table_name:
+------+-----------+
| id | name |
+------+-----------+
| 1 | Sindra |
| 2 | Auli |
| 3 | Brian |
| 4 | Bina |
| 5 | zian |
| 6 | Bri |
| 7 | Andre |
+------+-----------+
table_temp, id_temp_name foreign key of id(table_name) :
+------+--------------+
| id | id_temp_name |
+------+--------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 4 |
| 4 | 2 |
+------+--------------+
with this query :
SELECT *
FROM table_name
WHERE id IN
(SELECT id_temp_name FROM table_temp)
the result is always same look with table_name, i was looking for result that exactly same with id_temp_name order , so the result will be :
+------+-----------+
| id | name |
+------+-----------+
| 1 | Sindra |
| 3 | Brian |
| 4 | Bina |
| 2 | Auli |
+------+-----------+
thanks for any advice, .
You need to rewrite the query to be a JOIN between both tables, then you can set an ordering based on any column involved, even when not in the final result set:
SELECT table_name.id,
table_name.name
FROM table_name
INNER JOIN table_temp ON table_name.id = table_temp.id_temp_name
ORDER BY table_temp.id ;
Use a join instead of a sub-query.
SELECT table_name.id, table_name.name
FROM table_name
INNER JOIN table_temp ON table_name.id = table_temp.id
ORDER BY table_temp.id_temp_name
And... usually best to list the fields explicitly instead of using * to select all.
You should use a simple JOIN to achieve your result.
Your query:
SELECT *
FROM table_name
WHERE id IN
(SELECT id_temp_name FROM table_temp)
actually returns all the rows and columns of the table_name Table. So you won't get the desired id_temp_name results, since it's in a different table. That's why, you should use LEFT JOIN, since your left table is table_name, and your right table is table_temp, and you want to show data from a column of table_temp, which is id_temp_name.
So, what you need to do, is this:
SELECT tn.id, tn.name
FROM table_name AS tn
LEFT JOIN table_temp AS tt
ON tn.id= tt.id_temp_name
GROUP BY tn.id

LIMIT results to n unique column values?

I have some MySQL results like this:
---------------------------
| name | something_random |
---------------------------
| john | ekjalsdjalfjkldd |
| alex | akjsldfjaekallee |
| alex | jkjlkjslakjfjflj |
| alex | kajslejajejjaddd |
| bob | ekakdie33kkd93ld |
| bob | 33kd993kakakl3ll |
| paul | 3k309dki595k3lkd |
| paul | 3k399kkfkg93lk3l |
etc...
This goes on for 1000's of rows of results. I need to limit the number of results to the first 50 unique names. I think there is a simple solution to this but I'm not sure.
I've tried using derived tables and variables but can't quite get there. If I could figure out how to increment a variable once every time a name is different I think I could say WHERE variable <= 50.
UPDATED
I've tried the Inner Join approach(es) suggested below. The problem is this:
The subselect SELECT DISTINCT name FROM testTable LIMIT 50 grabs the first 50 distinct names. Perhaps I wasn't clear enough in my original post, but this limits my query too much. In my query, not every name in the table is returned in the result. Let me modify my original example:
----------------------------------
| id | name | something_random |
----------------------------------
| 1 | john | ekjalsdjalfjkldd |
| 4 | alex | akjsldfjaekallee |
| 4 | alex | jkjlkjslakjfjflj |
| 4 | alex | kajslejajejjaddd |
| 6 | bob | ekakdie33kkd93ld |
| 6 | bob | 33kd993kakakl3ll |
| 12 | paul | 3k309dki595k3lkd |
| 12 | paul | 3k399kkfkg93lk3l |
etc...
So I added in some id numbers here. These ID numbers pertain to the people's names in the tables. So you can see in the results, not every single person/name in the table is necessarily in the result (due to some WHERE condition). So the 50th distinct name in the list will always have an ID number higher than 49. The 50th person could be id 79, 234, 4954 etc...
So back to the problem. The subselect SELECT DISTINCT name FROM testTable LIMIT 50 selects the first 50 names in the table. That means that my search results will be limited to names that have ID <=50, which is too constricting. If there are certain names that don't show up in the query (due to some WHERE condition), then they are still counted as one of the 50 distinct names. So you end up with too few results.
UPDATE 2
To #trapper: This is a basic simplification of what my query looks like:
SELECT
t1.id,
t1.name,
t2.details
FROM t1
LEFT JOIN t2 ON t1.id = t2.some_id
INNER JOIN
(SELECT DISTINCT name FROM t1 ORDER BY id LIMIT 0,50) s ON s.name = t1.name
WHERE
SOME CONDITIONS
ORDER BY
t1.id,
t1.name
And my results look like this:
----------------------------------
| id | name | details |
----------------------------------
| 1 | john | ekjalsdjalfjkldd |
| 3 | alex | akjsldfjaekallee |
| 3 | alex | jkjlkjslakjfjflj |
| 4 | alex | kajslejajejjaddd |
| 6 | bob | ekakdie33kkd93ld |
| 6 | bob | 33kd993kakakl3ll |
| 12 | paul | 3k309dki595k3lkd |
| 12 | paul | 3k399kkfkg93lk3l |
...
| 37 | bill | kajslejajejjaddd |
| 37 | bill | ekakdie33kkd93ld |
| 41 | matt | 33kd993kakakl3ll |
| 50 | jake | 3k309dki595k3lkd |
| 50 | jake | 3k399kkfkg93lk3l |
----------------------------------
The results stop at id=50. There are NOT 50 distinct names in the list. There are only roughly 23 distinct names.
My MySql syntax may be rusty, but the idea is to use a query to select the top 50 distinct names, then do a self-join on name and select the name and other information from the join.
select a.name, b.something_random
from Table b
inner join (select distinct name from Table order by RAND() limit 0,50) a
on a.name = b.name
SELECT DISTINCT name FROM table LIMIT 0,50
Edited: Ahh yes I misread question first time, this should do the trick though :)
SELECT a.name, b.something_random
FROM `table` b
INNER JOIN (SELECT DISTINCT name FROM `table` ORDER BY RAND() LIMIT 0,50) a
ON a.name = b.name ORDER BY a.name
How this work is the (SELECT DISTINCT name FROMtableORDER BY RAND() LIMIT 0,50) part is what pulls out the names to include in the join. So here I am taking 50 unique names at random, but you can change this to any other selection criteria if you want.
Then you join those results back into your table. This links each of those 50 selected names back to all of the rows with a matching name for your final results. Finally ORDER BY a.name just to be sure all the rows for each name end up grouped together.
This should do it:
SELECT tA.*
FROM
testTable tA
INNER JOIN
(SELECT distinct name FROM testTable LIMIT 50) tB ON tA.name = tB.name
;

How to put filter condition on group_concat columns?

TABLE 1
+----+--------+
| id | name |
+----+--------+
| 2 | robin |
| 3 | jyothi |
| 1 | angel |
+----+--------+
TABLE 2
+----+---------+--------+
| id | hobbies | ref_id |
+----+---------+--------+
| 1 | walking | 1 |
| 2 | chating | 1 |
| 3 | reading | 2 |
| 4 | walking | 2 |
+----+---------+--------+
I want name & their hobbies who have hobby as 'walking' or name is 'robin'
+----+-----------------+
| name | hobbies |
+----+-----------------+
| 1 | walking,chating |
| 3 | reading,surfing |
+----+-----------------+
used query:
select name, group_concat(hobbies) as all_hobbies
from test1,test2
where test1.id = test2.ref_id
and test1.id in (select ref_id
from test2
where hobbies = 'walking')
group by name
I don't want to use subquery or save the entire result as virtual table & the search on all_hobbies though where clause. I want to do accomplish the group_concat columns through where clause or some way around as its increasing my SQL_JOIN_SZIE
I cannt use having here as that has to act as or condition for eg:
Either hobbies is 'walking' or name is 'robin'
I don't have mysql installed on this machine, but try this query out, I think it should work. You can think of the HAVING clause as a WHERE clause for your GROUP BY.
select name, group_concat(hobbies) as all_hobbies
from test1,test2
where test1.id = test2.ref_id and test1.name = 'robin'
group by name
having all_hobbies like '%walking%'
It can be accomplished by:
having all_hobbies like '%walking%' or
name like '%robin%'
having caluse will work on group concat colums & normal coumns
I'd join the two tables. Then id select the concatenation using (field a || field b) and place my restriction in the Where clause.