I have a table in sql ssms with values as follows:
RID | Comments| Date |Date Without Miliseconds
----------
780 |General Comment 2 |2016-11-20 14:12:44.097 |2016-11-20 14:12:44.000
----------
780 |General Comment 1 |2016-11-20 14:05:23.687 |2016-11-20 14:05:23.000
----------
780 |Document comment 1 |2016-11-20 14:05:23.687|2016-11-20 14:05:23.000
----------
780 |DEIRDRE Nominations1 |2016-11-20 14:05:23.390 |2016-11-20 14:05:23.000
So I want the output as follows:
----------
RID |List of Comments
----------
780 |General Comment 1,Document comment 1,DEIRDRE Nominations1; General Comment 2
I don't want to modify table schema. I added the Date without Miliiseconds column so that it will be easy to group by.
Comments from Same DateTime should be separated by comma and from different dateTime should be spearated by semicolon ';'
Can you help me with this?
Thanks,
Swarda
You can use GROUP_CONCAT for this.
SELECT RID, GROUP_CONCAT(Comments SEPARATOR ';') AS ListOfComments
FROM table
GROUP BY RID;
You need to use group_concat with concat to format the values, e.g.:
select id, concat(group_concat(comment), ';') as comments
from comments
group by id;
Here's SQL Fiddle.
Related
I have a table like this
item_id item_quantity
----------------------
324 2
432 23
543 12
879 3
The item_id field is not auto-increment but it is unique.
The inputs of the query will be some item_id values and the result should be sum of those in item_quantity.
For instance, if the inputs are 324, 543, then the query should return 14 (2 + 12)
Is it possible to do this at mysql level?
Try using in :
SELECT SUM(item_quantity)
FROM table
WHERE item_id in (324,543)
This works on MSSQL, not sure about MySQL, though:
SELECT SUM(item_quantity) FROM MyTable WHERE item_id in (324, 543)
EDIT: OK, others got the same answer. So I guess it also works on MySQL.
If all you need is the sum of the item_quantity, all you need to do is:
Select Sum(item_quantity)
From Table
Where item_id In (324, 543)
I need to output a purchase order into one line that has product and quantity of each product.
say
select product, quantity from order;
outputs
product A , 2
product B , 3
product C , 7
I need the output to be in one line:
product A,2,product B,3,product C, 7
Can this be achieved using mysql sql ?
try this:
SELECT GROUP_CONCAT(product,',' ,quantity) FROM ORDERS;
see the sqlfiddle:
http://sqlfiddle.com/#!2/d18e6/7
Check this Demo SQLfiddle
Query
SELECT GROUP_CONCAT(product, ',' ,quantity,'') FROM order1;
Result
|------------------------------------------------|
|GROUP_CONCAT(PRODUCT, ',' ,QUANTITY,'') |
|------------------------------------------------|
|product A,2,product B,3,product C,7 |
|------------------------------------------------|
I have a MySQL database with a table of survey responses with three important fields (renamed for clarity): SURVEY_TAKER_ID, QUESTION_NUMBER, and RESPONSE. Assuming a 3-question survey as an example, it would look something like this:
SURVEY_TAKER_ID | QUESTION_NUMBER | RESPONSE
----------------------------------------
101 1 Apple
102 1 Orange
103 1 Banana
101 2 Morning
102 2 Evening
103 2 Afternoon
101 3 Red
102 3 Blue
103 3 Yellow
I would like to create a SELECT query that outputs each survey taker's ID and responses in order by question number, e.g.:
101,Apple,Morning,Red
102,Orange,Evening,Blue
103,Banana,Afternoon,Yellow
I know that SQL Server has FOR XML, which can make this easier, but my database is in MySQL, and I must admit that I'm not really all that adept at SQL in the first place. Can anyone please give me a sample query that would produce the output above, or point me to a way to do it?
Many thanks to anyone who can help...
...Jay
SURVEY_TAKER_ID with "Question_Number: RESPONSE" style concating:
SQL Fiddle
SELECT SURVEY_TAKER_ID
,GROUP_CONCAT(CONCAT(QUESTION_NUMBER, ': ', RESPONSE) ORDER BY QUESTION_NUMBER SEPARATOR ', ') AS RESPONSES
FROM Table1
GROUP BY SURVEY_TAKER_ID
ORDER BY SURVEY_TAKER_ID
.
SURVEY_TAKER_ID with "RESPONSE" alone style concating:
SQL Fiddle
SELECT SURVEY_TAKER_ID
,GROUP_CONCAT(RESPONSE ORDER BY QUESTION_NUMBER SEPARATOR ', ') AS RESPONSES
FROM Table1
GROUP BY SURVEY_TAKER_ID
ORDER BY SURVEY_TAKER_ID
Try this:
SELECT CONCAT(SURVEY_TAKER_ID, ',', RESPONSE)
FROM (SELECT SURVEY_TAKER_ID, GROUP_CONCAT(RESPONSE) RESPONSE
FROM (SELECT * FROM SURVEY ORDER BY SURVEY_TAKER_ID, QUESTION_NUMBER) A
GROUP BY SURVEY_TAKER_ID
) A
I want to count string separators from a MySQL query, mean if the field value is
like :-
1,2,3,4,5
as the string is comma separated so the separator count will be 4.
any idea then please share
THANKS,
you can try to count the length of string and minus the length of string without commas as follows:
LENGTH('1,2,3,4,5') - LENGTH(REPLACE('1,2,3,4,5', ',', ''))
select length('1,2,3,4,5') - length(replace('1,2,3,4,5', ',', ''))
I suggest the following design :
Table name : USER_HOBBIES
| USER_ID | HOBBY_ID |
1 1
1 2
1 3
2 2
2 4
2 5
And now you can easily count user hobbies for a given user :
SELECT count(*) FROM USER_HOBBIES WHERE USER_ID = <user-id>
although it requires another table it is much clearer and on a long list of hobbies this will be much faster than using a function for manipulating strings.
I have to add two or more strings from MySQL database, for this I used CONCAT() function.
Here is the first table classes which stores PHP classes.
class_id class_name
-------- ----------
1 accountant
2 attendance
Another table methods which stores each class methods.
class_id method_name
-------- -----------------------
1 __construct
1 add_expenses
2 __construct
2 attendance_report
And I write the query for concatenation.
SELECT
`cc`.`class_id`,
`cc`.`class_name`,
CONCAT(`cm`.`method_name`, ',') AS `method_name`
FROM
`classes` AS `cc`
LEFT JOIN `methods` AS `cm`
ON `cm`.`class_id` = `cc`.`class_id`
GROUP BY `cc`.`class_name`;
Which is not working. My expected output is
class_id class_name method_name
-------- -------------- ------------
1 accountant __construct, add_expenses, .... n
2 attendance __construct, attendance_report, .... n
Any ideas?
Use
GROUP_CONCAT
instead of
CONCAT
GROUP_CONCAT(cm.method_name) you do not needs to pass comma as separator that will be taken default.