Combine multiple rows (of one column) from mysql request into one field - mysql

I'd like to gather the result of one query into one single field if possible.
The request is :
select group_concat(col1) from table1
group by col3, col2
having count(*)>1
The result is like :
'123','124','125'
'123','125'
'126','127'
'123','127'
The result I'm looking for :
'123','124','125','123','125','126','127','123','127'
I tried to use again my group_concat, the concat fonction, or to use this whole query as a subquery without much success...

You're looking for the GROUP_CONCAT aggregation function, which you found. But you need to group the intermediate results again.
Apparently you have tried that, but since you didn't post your query, I'll give it to you:
select group_concat(col1intermediate) as col1total
from
(select group_concat(col1) as col1intermediate
from table1
group by col3, col2
having count(*)>1) as alias_subquery
I don't know why your own attempt failed. Maybe you forgot to add an alias (col1intermediate) to the aggregated column?

Related

What is the correct MySQL syntax for a query with a COUNT from another table as a column?

Here is a query I've used that almost does what I want:
SELECT *, COUNT(p.prize_id) as number_prizes
FROM tbl_draw d
INNER JOIN tbl_prize p ON p.draw_id=d.draw_id
WHERE d.draw_id={$draw_id}
The key point is that it counts the number of items from tbl_prize that matches on draw_id and presents that number as a new column 'number_prizes' in the result set. For this query, the result set is a single row, because of the final WHERE clause that matches on a specific draw_id.
I want it to return ALL of the rows from tbl_draw with that same calculation per row. My problem is that when I remove the final clause "WHERE d.draw_id={$draw_id}", the result collapses all the rows into one, and is only sending back the first such row found in tbl_draw and with 'number_prizes' being a total of all
How can I phrase the query better?
You need to add a group by clause, naming all columns, eg:
<your query>
group by col1, col2, ... -- list all other columns
As said above, you should use the group by after the end of your query, like this:
SELECT *, COUNT(p.prize_id) as number_prizes
FROM tbl_draw d
INNER JOIN tbl_prize p ON p.draw_id=d.draw_id
WHERE d.draw_id={$draw_id}
GROUP BY col1, col2, col3, coln.....
And, if you want the id from tbl_draw, your SELECT must be like:
SELECT d.id as draw_id, *, COUNT(p.prize_id) as number_prizes
I strongly recommend that you do not use SELECT *, ... here. List all of the columns that form the groups within the SELECT clause, then repeat that list within GROUP BY.
Here's why: "SELECT * will work only until someone adds a column to that table!" Which of course will happen someday.
Then, all of the sudden, your query will start failing, because * references this new column (which presumably you don't care about anyway ...), which is not in the GROUP BY list as is required.
Always specify exactly what columns (and aggregate functions ...) your query needs. You'll be very glad you did. (And so will your co-workers!)

MYSQL nested select query from same table

What I am trying to do is select each distinct column1 value from table1 and then select all the columns from those rows returned from the above. Is this possible at all?
What I have so far, however, nothing is returned:
SELECT * FROM (SELECT DISTINCT column1 FROM table1)
I've thought about putting a unique/distinct restriction in the where clause of the query:
SELECT * FROM table1 WHERE some_unique_determiner column1
Any ideas how I could go about achieving the desired output?
Ok so answering my own question. What I need to do was to group the data by column1, without use of a nested query. Many thanks to #VR46 for the help.
SELECT * FROM table1 GROUP BY column1
Returned all columns from each unique value from column1
In your next posts, it will be better if you post your table structures, input and desired out put so it will be easier for us to understand.
If I did understand, there is one of two options:
Either you have duplicates, and you want to eliminate them so your correct query should be
select distinct COLUMNa,COLUMNb,COLUMNc... ETC
which will drop duplicates(that the entire row is the same).
Or you want to eliminate rows that have the same column1 and it doesn't matter if all the rest is the same or not.
In that case, You need to tell us which one of the result you want to keep, The up to date one,the older, random ETC.. because right now its impossible to make you a query that selects all the columns after you distinct, since all the duplicates will return like this:
SELECT * FROM TABLE WHERE COLUMN1 IN(SELECT DISTINCT COLUMN1 FROM TABLE)
Which is a wrong query since it doesn't do anything.

Simplify select with count

I need to get some values from a database and count all rows.
I wrote this code:
SELECT author, alias, (select COUNT(*) from registry WHERE status='OK' AND type='H1') AS count
FROM registry
WHERE status='OK' AND type='H1'
It works, but, how can I simplify this code? Both WERE condition thereof.
If the query is returning the resultset you need, with the "total" count of rows (independent of author and alias), with the same exact value for "count" repeated on each row, we could rewrite the query like this:
SELECT t.author
, t.alias
, s.count
FROM registry t
CROSS
JOIN ( SELECT COUNT(*) AS `count`
FROM registry c
WHERE c.status='OK'
AND c.type='H1'
) s
WHERE t.status='OK'
AND t.type='H1'
I don't know if that's any simpler, but to someone reading the statement, I think it makes it more clear what resultset is being returned.
(I also tend to favor avoiding any subquery in the SELECT list, unless there is a specific reason to add one.)
The resultset from this query is a bit odd. But absent any example data, expected output or any specification other than the original query, we're just guessing. The query in my answer replicates the results from the original query, in a way that's more clear.
try this:
SELECT author, alias, count(1) AS caunt
FROM registry
WHERE status='OK' AND type='H1'
group by author, alias
The status and type are the criteria to extract the fields from the table. You cannot go more simpler than this.
Try using a group by,
SELECT author, alias, COUNT(*) AS COUNT
FROM registry
WHERE STATUS='OK' AND TYPE='H1'
GROUP BY author, alias
Depending on how your table is set up, you may actually need to do this to get the data you are looking for.
Some good reading for this: https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
However, if you are wanting the count to be simply ALL the records with that criteria, what you have may be simplest already.

GROUP BY clause with non aggregate functions

Why mysql allows use non aggregate functions with GROUP BY clause ?
For example, this query works fine:
SELECT col, CHAR_LENGTH(col) FROM table
GROUP BY col
There is acceptable using querys like this ?
Sometimes is quite acceptable. Your query, written in more standard SQL, would be something like:
SELECT col, CHAR_LENGTH(col)
FROM (SELECT col FROM table GROUP BY col) c
or as:
SELECT col, MAX(CHAR_LENGTH(col))
FROM table
GROUP BY col
using non aggregate functions you can simplify the query a little bit, but the query would be a little more difficult to read.
It could also be useful when you are sure that all non aggregated columns share the same value:
SELECT id, name, surname
FROM table
GROUP BY id
HAVING COUNT(*)=1
or when it doesn't matter which value you need to return:
SELECT id, name
FROM table
GROUP BY id
will return a single name associated to that id (probably the first name encountered, but we can't be sure which one is the first, order by doesn't help here...). Be warned that if you want to select multiple non aggregated columns:
SELECT id, name, surname
FROM table
GROUP BY id
we have no guarantees that the name and surname returned will belong to the same row.
I would prefer not to use this extension, unless you are 100% sure of why you are using it.
MySQL has some "improvements" and tries to run and return result from invalid queries, in example like yours every good RDBMS should throw syntax error, but MySQL will run it, group the result by col and put value of randomly chosen row into second column.
If I'm guessing correctly about what you want to do, DISTINCT is a better choice:
SELECT DISTINCT col, CHAR_LENGTH(col) FROM table;
It more clearly indicates the readers what you're trying to accomplish.
Here is a SQLFiddle.

MySQL GROUP BY Multiple Columns

I need to perform a GROUP BY on 2 columns separately...
In common terms, I'd like the query to say: GROUP BY column 1, then once this grouping has been performed, and the rows returned have been refined, go back to the top and GROUP BY column 2 to refine the rows returned again.
For instance, instead of stating:
GROUP BY column_1, column_2
I want to state (I Understand this is incorrect syntax):
GROUP BY column_1
GROUP BY column_2
If this is unclear I can include a sample query with expected returned results.
Are you trying to do something like this?
select ...
from (
select ...
from some_table
where ...
group by column1
) as dt
group by column2
That's the closest thing I can think of that matches what your question appears to be asking.
Mostly you can group by multiple columns in mysql. The query is:select * from table group by col1, col2
But you can't get answer as you want as. So you've another chance to get correct answer in mysql. That is, you've to use subqueries.select * from (select * from table group by col2) tabl group by col1