This is my query:
SELECT
store_id as `initial`,
CONCAT(',', store_id, ',') AS store_id
FROM `mytable` AS `main_table`
WHERE (`store_id` LIKE '%,13,%');
These are the results without the where conditions:
When I executed my query , I got no results. Same for WHERE (store_idLIKE '%,3,%'); Is it some kind of rule or exception for the , and like operator ?
Thank you :)
Using FIND_IN_SET fixed my problem. More Info:
https://www.w3resource.com/mysql/string-functions/mysql-find_in_set-function.php .
Credit goes to #Jens
SELECT
store_id as `store_id`,
FROM `ffm_nonpayment_actiontemplate` AS `main_table`
WHERE FIND_IN_SET('13', store_id);
The problem is that field store_id in WHERE has initial value and not the value you calculate with CONCAT(',', store_id, ',') AS store_id
If you want filter you can use HAVING keyword
Related
I am writing the mysql query where I need to compare data entered by user with the sku column in database tab
Query is like
SELECT *
FROM `is_product_info`
WHERE REPLACE( '-', '', sku ) LIKE '%ETI2006%'
so that if sku in database contains "-" in sku, I am replacing all hyphens with "" before comparing.
so whether sju no contains ETI2006 or ETI-2006, it will come in output
I think you may just like this
SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'
I didn't try on a running MySQL but this should work:
SELECT *
FROM `is_product_info`
WHERE sku REGEXP REPLACE('ETI-2006','-','-?');
I made the mistake in replace syntax, it works with the below one:
SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'
Using replace() on every row is what's slowing it down.
All of these approaches should be faster - see which one works best for you:
Option 1 - use LIKE twice:
WHERE sku LIKE '%ETI-2006%'
OR sku LIKE '%ETI2006%'
Option 2 - Use RLIKE once:
WHERE sku RLIKE 'ETI-?2006'
I have data in column which I want to select with substring_index and group by the result of substring. Is it possible to make in one query?:
Example
code:
R0001.10
R0001.20
R0002.10
R0002.30
If use
SELECT SUBSTRING_INDEX(code, '.', 1) FROM products;
It goes like this:
R0001
R0001
R0002
R0002
But when I use
SELECT SUBSTRING_INDEX(code, '.', 1) FROM products GROUP BY code;
It gave some strange result
01
01
010210000
0103020
etc.
There is no issue using GROUP BY clause check SQL Fiddle. Might be other issue.
I have a table with languages which s_name value looks like this:
'en_UK'
'en_US'
'de_CH'
'de_AT'
I want to get all the distinct languages, without the country part. So for example, in case I just had those of the example, I would need to get:
en
de
What would be the best way of doing so?
I have this right now:
SELECT DISTINCT SUBSTRING(name,1,2)
FROM my_languages
Try this:
SELECT DISTINCT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
OR
SELECT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
GROUP BY langName
Check this link MySQL STRING Functions
Here is a simple way:
select distinct left(s_name, 2)
from t
This assumes the language name is the left two characters.
This will work in Oracle as well as in MySql I think:
SELECT SUBSTR('en_UK',INSTR('en_UK','_')+1) country FROM dual
/
I'm probably having a no-brain moment.
I want to return a series of numbers using GROUP_CONCAT from two fields in my database. I have done this so far using the following:
SELECT t_id,
CONCAT(GROUP_CONCAT(DISTINCT s_id),',',IFNULL(GROUP_CONCAT(DISTINCT i_id),'')) AS all_ids
FROM mytable GROUP BY t_id
This works fine but if i_id is NULL then of course I get an unnecessary comma. Is there a better way to do this so I don't end up with a comma at the end if i_id is NULL?
You need to use CONCAT_WS to avoid extra comma for NULL values, try this:
SELECT t_id,
CONCAT_WS(',', GROUP_CONCAT(DISTINCT s_id),
GROUP_CONCAT(DISTINCT i_id)) AS all_ids
FROM mytable
GROUP BY t_id;
I'm running a query to analyse variations in Group_member_ID for some users.
What I would like to identify is the key variations in group_member_ids (eg Group1 on 01/08/2011, Group5 on 05/08/2011).
I came up with this command:
select id,
CAST(group_concat(concat('[',group_member_id,'-',from_unixtime(obs_time),']') order by obs_time) as CHAR(10000) CHARACTER SET utf8)
from Table1
where id=1
RESULT:
imei group/date
1[178-2011-06-13 18:58:31],[0-2011-06-13 19:20:56],[0-2011-06-17 17:21:57],[0-2011-06-19 16:53:29],[0-2011-06-22 16:41:11],[178-2011-09-30 16:43:11],[179-2011-10-01 18:43:11]
How can I eliminate the Group/date [0-2011-06-17 17:21:57],[0-2011-06-19 16:53:29],[0-2011-06-22 16:41:11] from this query as I already identified the first record for group_member_id=0 and the others do not matter for me...
In other words, I would like my final result to look like:
imei group/date
1[178-2011-06-13 18:58:31],[0-2011-06-13 19:20:56],[178-2011-09-30 16:43:11],[179-2011-10-01 18:43:11]
I'm stuck. I was thinking of using LIMIT in my group_concat but apparently it's not possible. Or is it?
Thanks for your answers.
TRY with GROUP BY
SELECT id, CAST( GROUP_CONCAT (
CONCAT('[',group_member_id,'-',from_unixtime(obs_time),']')
ORDER BY obs_time ) as CHAR(10000) CHARACTER SET utf8)
AS group_set
FROM table1
WHERE id=1
GROUP BY group_member_id