group_concat text field in mysql - mysql

Tried to group_concat a text field in mysql.But its taking only the first value.(if the values are '177,178') it taking only 177 because I am saving this as a text field.So how can I do group_concat with this fields?
My query looks as such:
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ') FROM abc WHERE abc.lookupId IN ('177,178')

Are you misplacing the quotes within your IN?
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ',') FROM abc WHERE abc.lookupId IN (177,178)

The problem isn't with GROUP_CONCAT, the problem is that your WHERE clause is only selecting one row. Since the lookupId field is an integer, it's converting the string '177,178' to an integer, which is just 177. You shouldn't have quotes around the values in the IN() list, that's making it just a single value to look for.
SELECT GROUP_CONCAT(abc.displayValue SEPARATOR ' ')
FROM abc
WHERE abc.lookupId IN (177, 178)
If the comma-separated string is actually coming from a column in a table you're joining with, see sql join tables where 1 column has comma.
SELECT GROUP_CONCAT(table1.displayValue SEPARATOR ' ')
FROM table1
JOIN table2 ON FIND_IN_SET(table1.lookupID, table2.lookupIDs)
WHERE ...

Related

Group Concat Two Values Into One field

Group concat is driving me nuts.
I have 1 table 2 columns
FRUIT QUANTITY
APPLE 4
ORANGE 6
I'd like to group concat these into one field
orange:4, apples:6
You can concat() both columns for each row, then group_concat() the results over all rows:
select group_concat(concat(fruit, ':', qty) separator ', ') res from mytable
You might want to consider adding an order by to group_concat() in order to get predictable, consistent results.
You can list multiple columns and other values in the GROUP_CONCAT() call, and it will concatenate them together.
SELECT GROUP_CONCAT(fruit, ':', qty SEPARATOR ', ') AS fruits
FROM yourTable
If you have lots of columns that you're joining like this, you could use CONCAT_WS() to concatenate them all together with the same delimiter. See GROUP_CONCAT multiple columns as an array or exolodable string

If value is present in stored text string

I have a table, one of the columns contains a text values, some of which are comma separated string, like this:
Downtown, Market District, Warehouse District
I need to modify my query to see is a given value matches this column. I decided that using IN() is the best choice.
SELECT *
FROM t1
WHERE myValue IN (t1.nighborhood)
I am getting spotty results - sometimes I return records and sometimes not. If there's a value in t1.nighborhood that matches myValue, I do get data.
I checked and there are no MySQL errors. What am I missing?
You can use FIND_IN_SET() to search a comma-delimited list:
SELECT *
FROM t1
WHERE FIND_IN_SET(myValue, REPLACE(t1.nighborhood, ', ', ','));
The REPLACE() is necessary to remove the extra spaces.
Another solution is to use regex to match your search value surrounded by commas if necessary:
SELECT *
FROM t1
WHERE t1.nighborhood REGEXP CONCAT('(^|, )', myValue, '(, |$)');
In general, it's bad design to store distinct values in a single column. The data should be normalized into a related table with a foreign key.

remove whitespace in between Column values in mySQL

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:]]+', '');

MySql find a value in a comma separated set

I have values stored like this in a field 1,255,230,265.
Is there a function in MySQL that will give me the second value in that string? In this case it'll be 255.
I tried using locate, but that does not seem to be meant for this.
Try this
select SUBSTRING_INDEX(SUBSTRING_INDEX(field_name,',',2),",",-1) from table_name
You might want to use SUBSTRING_INDEX() function.
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field,',',2),',',-1)
FROM yourTable.
This grabs everything infront of the second comma, then grabs everything after the last comma (-1)
Try This
select * from Table1 where ',' + Nos+ ',' like '%,255,%'
Refer:
MySQL query finding values in a comma separated string
mysql check if numbers are in a comma separated list
Use FIND_IN_SET() function:
SELECT *
FROM tableA
WHERE FIND_IN_SET(255, columnName)
OR
Use LIKE operator
SELECT *
FROM tableA
WHERE CONCAT(',', columnName, ',') LIKE '%,255,%'

Concat Values In MySQL Query(To Handle Null Values)

I am writing a PHP and MySQL application in which i have to concatenate multiple column values into one single column.I would have used the concat() function,but it does not handle null values,and the concat_ws(),which does not return the result in the output i want.
What i need can be achieved in the Oracle database like this:
Select 'The Surname Is'||last_name from employees;
My Issue is how can i achieve this same result with MySQL..without using the above named functions?
CONCAT with IFNULL:
SELECT
CONCAT('The Surname Is ', IFNULL(last_name, 'sadly not available'))
FROM `employees`
#Minesh: CONCAT_WS does not 'take care' of NULL values. To illustrate this...
CONCAT_WS("~",house.name,house.address,house.type)
In the above example, if house.address is NULL the returned result will not contain a neat double tilda (~~) as expected. It will be a tilda separated list with only 1 tilda. eg "fun House~mansion"
You can also use CONCAT_WS function which takes care of NULL values
SELECT
CONCAT_WS(' ','The Surname Is',lastname)
FROM `employees`
Use coalesce to concat an empty string
select concat(coalesce(null, ''));
A little trick: Use empty string like separator with CONCAT_WS (Some times you wan't insert white spaces)
CONCAT_WS('','The Surname Is:',lastname)
FROM `employees`