Mysql remove special characters from a set - mysql

I want to remove a spacial character in my query can anyone help. This is my query
select sum(value) from table_1 where id in (1, 2,);
This 1,2, is fetch from other table using sub-query.

To remove the trailing colon, you can use trim():
SELECT TRIM(TRAILING ',' FROM '1,2,');

My guess is that you want to look for individual values in the list, especially because ids don't usually contain commas.
For that, you can do:
select sum(value)
from table_1
where find_in_set(id, '1, 2,') > 0;
If the values are coming from a subquery, you would be better off using the subquery directly (in most cases). The query would be something like:
select sum(value)
from table_1
where id in (<subquery>);
You would need to modify the subquery to return a list of ids, rather than all concatenated into one field.

Related

MySQL When to use quotation after SELECT AS

After reviewing the use of SELECT in mysql, I found after as, sometimes without single quotation and sometimes has.
For example:
SELECT * AS DAY
compare to:
SELECT * AS 'Cancellation Rate'
So when to use single quotation after SELECT AS?
for composite name eg: Cancellation Rate.. use backtics not quotes
select my_col_name as `Cancellation Rate`
from my_table
The proper syntax would be something like:
SELECT column_name AS colname FROM table_name
As mentioned in the comment, you cannot not alias a 'select all', which is what * represents. It selects ALL columns from your table.
You can also alias a table's name, like:
SELECT * FROM employees e WHERE column_name = 1;
When you alias a table's name, it can be easier to read in larger and more complex queries such as Joins.
You can get a better idea of all the possibilities by exploring this page https://dev.mysql.com/doc/refman/8.0/en/select.html, plenty of fairly easy to follow examples.

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.

How to concat two row data in mysql if they are the same

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;

Query MySQL field for LIKE string

I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'

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,%'