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.
Related
I want to fetch all the employee's name which are started from A or B or C.
Also i don't want to use Union or OR conditions.
NOTE: I don't want these two solution
CASE 1. (like '%A' or like'%B' or like '%C').
CASE 2. fetching separately and using union combining.
I have created sample data here sample link Please find it.
Try this
Select * from employee where substring(emp_name, 1, 1) in ('A', 'B', 'C');
You can try the following:
select * from employee where emp_name REGEXP '^[a-c]'
SQLFIDDLE DEMO
Use this
select * from employee where emp_name REGEXP '^[abc]'
Fiddle Example
Another way you can use or
select * from employee where emp_name REGEXP '^[a or b or c]'
SQL Fiddle
You can use the Regular expression for this. The solution to your question is
select * from employee where emp_name REGEXP '^[ABC]'
SQL FIDDLE DEMO
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
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 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
/
This is a query I've been puzzling over for quite some time, I've never been able to get it to work quite right and after about 40 hours of pondering I've gotten to this point.
Setup
For the example issue we have 2 tables, one being...
field_site_id field_sitename field_admins
1 Some Site 1,
2 Other Site 1,2,
And the other is admins like...
field_user_id field_firstname field_lastname
1 Joe Bloggs
2 Barry Wills
Now all this query is designed to do is the following:
List all sites in the database
Using a JOIN and FIND_IN_SET to pull each admin
And GROUP_CONCAT(field_firstname, ' ', field_lastname) with a GROUP BY to build a field with the real user names.
Also allow HAVING to filter on the custom result to narrow the results down further.
All this part works perfectly fine.
What I can't work out how to achieve is to sort the results by the GROUP_CONCAT result, I imagine this is being the ORDER BY works before the concat function therefore the data doesn't exist to order by it, so what would the alternative be?
Code examples:
SELECT *
GROUP_CONCAT(DISTINCT field_firstname, ' ', field_lastname ORDER BY field_lastname SEPARATOR ', ') AS field_admins_fullname,
FROM `table_sites`
LEFT JOIN `table_admins` ON FIND_IN_SET( `table_admins`.`field_user_id`, `table_sites`.`field_site_id` ) > 0
GROUP BY field_site_id
I also tried a query that used a subquery to gather the group_concat result as below...
( SELECT GROUP_CONCAT(field_firstname, ' ', field_lastname ORDER BY field_lastname ASC SEPARATOR ', ') FROM table_admins
WHERE FIND_IN_SET( `table_admins`.`field_user_id`, `table_sites`.`field_admins` ) > 0
) AS field_admins_fullname
Conclusion
Either way attempting to ORDER BY field_admins_fullname will not create the correct results, it won't error out but assume that's because the given ORDER BY is blank so it just does whatever it wants.
Any suggestions would be welcome, if this is just not possible, what would be another recommend index methodology?
Two things I see wrong:
1st, is the JOIN. It should be using s.field_admins and not field_site_id :
ON FIND_IN_SET( a.field_user_id, s.field_admins ) > 0
2nd, you should use the CONCAT() function (to conactenate fields from the same row) inside the GROUP_CONCAT().
Try this:
SELECT s.field_site_id
, s.field_sitename
, GROUP_CONCAT( CONCAT(a.field_firstname, ' ', a.field_lastname)
ORDER BY a.field_lastname ASC
SEPARATOR ', '
)
AS field_admins_fullname
FROM table_sites s
LEFT JOIN table_admins a
ON FIND_IN_SET( a.field_user_id, s.field_admins ) > 0
GROUP BY s.field_site_id
Friendly advice:
Don't use Do use
------------ --------
table_sites site
table_admins admin
field_site_id site_id
field_sitename sitename
field_admins admins
But what should really be stressed, is your setup. Having fields that have comma separated values lead to this kind of horrible queries that use FIND_IN_SET() for joins and GROUP_CONCAT() for showing results. Horrible to see, difficult to maintain and most important, very, very slow as no index can be used.
You should have something like this instead:
Setup suggestion
Table: site
site_id sitename
1 Some Site
2 Other Site
Table: site_admin
site_id admin_id
1 1
2 1
2 2
Table: admin
user_id firstname lastname
1 Joe Bloggs
2 Barry Wills
I think you need to repeat the complex CONCAT statement you are selecting within the ORDER BY.
So your order by would be more like...
ORDER BY (GROUP_CONCAT(DISTINCT field_firstname, ' ',
field_lastname ORDER BY field_lastname SEPARATOR ', ')) ASC
I have not tried this but I had a similar issue which this seemed to solve but it was much simpler without the DISTINCT etc.
wrong group by, try this ?
GROUP BY field_site_id