I am trying to update 1 column with the results of concatenating several rows.
I am able to do it in a Select query but can't figure it out in an Update query.
SELECT GROUP_CONCAT(SizeTemp SEPARATOR ', ') FROM Table GROUP BY ParentSKU
Also, the result separates everything with a coma which is fine but I need the last string NOT to be followed by a coma. In the example below: no coma after XL
S,M,L,XL
Thanks for any help.
You can use update with inner query as shown below:
UPDATE TABLE AS t1,
(SELECT ParentSKU, GROUP_CONCAT(SizeTemp SEPARATOR ', ') AS sizes FROM TABLE GROUP BY ParentSKU) AS t2
SET t1.sizes = t2.sizes
WHERE t1.ParentSKU = t2.ParentSKU
AND t1.ParentSKU = ?
Criteria/column may differ based on which column needs updating.
Related
Let's assume I have a table which store register user data, the records might have same registered name but different email, like following:
I want to create a front view to manipulate those data but I don't want those same name show repeatedly, can mysql statement query to output result like
this is the result so far I can do but it can't bind same name into one.
select * from `register`
where `fullname` in (
select `fullname` from `register`
group by `fullname` having count(*) > 1
)
One thing you could do is to do a SELECT DISTINCT on the duplicate row, and make use of the GROUP_CONCAT(); function in MYSQL to concatenate your desired values into one row, and GROUP BY fullname to get the order you wanted.
Note that I am also putting the user ids into a grouped row, so that you can track which ids belong to which name.
SELECT
DISTINCT fullname as full_name,
GROUP_CONCAT(id SEPARATOR ', ') as user_ids,
GROUP_CONCAT(email SEPARATOR ', ') as emails
FROM
tbl_register
GROUP BY
tbl_register.fullname
Working SQL Fiddle
This would be the logical way to do it. Hope this helped. :)
More information on the GROUP_CONCAT(); function here: https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Try this:
SELECT DISTINCT *duplicate_column* FROM *table_name1* WHERE *col_id* IN (SELECT *cols_to_dusplay* FROM *table_name1* GROUP_BY *duplicate_column*
How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');
I have a mysql table where I some data are repeated in one column but have different value in another. I want to concat them an them and create new string.
I am getting data in following format:
See rows with checkbox option and vodaphoneissues and comments are repeated twice, yet have different values in another column.
I want to concatenate that value with comma separated format.
There is a function called GROUP_CONCAT.
I do not know names of your columns, let's say they are COLUMN1, COLUMN2, etc. The code will be:
SELECT COLUMN3, GROUP_CONCAT(COLUMN4) FROM your_table GROUP BY COLUMN3;
Use GROUP_CONCAT for that purposes
SELECT id, GROUP_CONCAT(DISTINCT column_name ORDER BY column_name SEPARATOR ', ')
FROM table
GROUP BY comments, versions;
Also pay attention to GROUP BY clouse as it groups by few columns:
GROUP BY comments, versions;
I have written this simple query which would pull out all the data from table into a CSV file.
SELECT Group_concat(Concat(column_name))
FROM information_schema.columns
WHERE table_name = 'subject_assignment'
AND table_schema = 'newschema2'
UNION ALL
SELECT (SELECT Group_concat('`', column_name, '`')
FROM information_schema.columns
WHERE table_name = 'subject_assignment'
AND table_schema = 'newschema2')
FROM subject_assignment
INTO OUTFILE 'D:\\export\\asd.csv'
Now, the first bit works great but I have issues with the second part.
Instead of pulling out data from columns specified in column list it just displays me all the column names over and over again.
Could you please suggest what I am doing wrong?
Thanks.
In your second SELECT you do not select any column from subject_assignment. Instead, you're selecting single string value made from concatenated column names. And you're selecting it as many times as the row count of subject_assignment.
UPDATE:
If you want to dynamically create column names and then select data from them, see this: https://stackoverflow.com/a/17573774/925196
I am unsure how to change a select query to an update.
The select query is:
SELECT IF (strcmp( `player1`, `player2` ) >0, concat(player2, " - ", player1), pair) as GoodPair from scores
The problem I am trying to solve is that the pairs field now contains pair names that are not in the same order. For example "Bill - Dennis" and "Dennis - Bill" for the same pair appear in different records and I want the pair names to be always with the "lesser" name first so that I can count the times that a particular duo is found.
If more info is needed, let me know.
Thanks in advance.
Bili
You can use LEAST and GREATEST functions:
SELECT
CONCAT_WS(' - ', LEAST(payer1,player2), GREATEST(player1,player2)) as GoodPair
FROM
scores
I also prefer to use CONCAT_WS to concatenate strings with a separator instead of CONCAT but in this particular case it is the same.
To create an update query, I would use this:
UPDATE scores
SET
GoodPair = CONCAT_WS(' - ', LEAST(payer1,player2), GREATEST(player1,player2))
If you just need do swith player1 with player2 in case that player1 is greater than player2, you could use this UPDATE query:
UPDATE score
SET player1=LEAST(player1,player2),
player2=GREATEST(player1,player2)
WHERE
player1>player2
Fiddle is here.