compare values between comma in mysql - mysql

I have table like:
businesses:
id name tags
1 ken my, oh, abc
2 jen city, cle, dir
3 sen state, NY, irs
...
I want to create query like:
select * from businesses where name like 'ken' and tags = 'oh'
How can i get value between first and second comma?

select * from businesses where
name like 'ken' and FIND_IN_SET('oh',tags);

Related

Search words in any order using "Like" query

I have a students table with following rows in it.
ID
Name
1
John Smith
2
Hellen Fernandis
3
Ali Abbas
4
Sameer Khan
I want that even if I use the below mentioned query
Select * from students where name like '%Abbas Ali%'
Row No. 3 should come in result.
You can't do it in a single LIKE, you need multiple conditions:
WHERE name LIKE '%Abbas%' AND name LIKE '%Ali%'
If you to do it in other manner, just try this:
SELECT * FROM students WHERE name LIKE '%Abbas' AND name LIKE 'Ali%'
Select * from students where name like '%Abbas%Ali%'

How to sort based on keyword first?

Here is a sample Database
First_name country
Andy US
Manu India
Paul Pakistan
Ramesh Pakistan
Rich India
So, what i want is to select all records from the above table and display according to name.
Like :-
I want to select person name to be display first whose country name is India and after US, Pakistan.
How can i perform this task in single SQL query ?
Update
I don't know how many Country are there.
Country the need to be display first will be input by the user.
Use a CASE statement to give each record a sortkey. 0 for a country match, 1 for a mismatch, so the desired country comes first.
select *
from mytable
order by case when country = #country then 0 else 1 end, first_name
May be something like this
Select * From Table1
Order By CASE WHEN country = 'INDIA' THEN 0
WHEN country = 'US' THEN 1
Esle 2
END;
Or You can use FIELD
Select * From Table1 Order By FIELD(country, 'India', 'US', 'Pakistan') ;
Use FIELD Function
Try this:
SELECT fitst_name, country
FROM tableA
ORDER BY FIELD(country, 'India', 'US', 'Pakistan'), fitst_name

MySQL query: use each result/row to regex and count another table

Not sure if this is possible. With two tables, one is country codes:
e.g.
id | code | country
1 .us United States
2 .ru Russia
And so on (about 200+ rows)
The other is URLs:
http//:example.gov.us
http://example.gov.ru/index.php
http://xyz.gov.us/test.html
And so on.
I don't know what URLs will come in, so I would have to grab each country code and somehow query the URLs for any matches against the country codes and count how many there are for each.
e.g (?)
gov.[country code]
Ideally, I would like the output to be grouped by country name and counted, something like, using the above URLs as an example, it might result in:
country | total
United States | 2
Russia | 1
Like I said, not sure if this can be done in MySQL with regex, substrings etc. Would love to know if it can be.
You could use a query like this:
SELECT
c.country,
COUNT(*)
FROM
countries c INNER JOIN URLS u
ON SUBSTRING_INDEX(SUBSTRING_INDEX(url, 'http://', -1), '/', 1)
LIKE CONCAT('%', c.code)
GROUP BY
c.country
Please see fiddle here.
Using SUBSTRING_INDEX(url, 'http://', -1) you can get the whole string after the http://
http://example.gov.ru/index.php ---> example.gov.ru/index.php
then using SUBSTRING_INDEX(..., '/', 1) on this string you can get the part of the string before the first / or the whole string if there's no /
example.gov.ru/index.php ---> example.gov.ru
you can then check if example.gov.ru LIKE '%.ru'
select country, count(*) total
from country_codes c
join urls on urls.url RLIKE CONCAT("^http://[^/]+\\.gov\\.", c.code, "($|/)")
group by county

Grouping Mysql query contacts table by city

an easy one, its long time i do not use mysql queries so maybe someone can help me out with this nooby question, i have a contacts table, id, name, and city.. i want to get all the contacts listed as follows.
city 1
--------
contact1
contact2
contact3
city 2
--------
contact4
contact7
contact10
city 3
--------
contact5
contact6
contact8
I do not want to use any additional php coding, just get the sql result like this
city 1
-> contact 1
-> contact 2
-> contact 3
city 2
-> contact 4
-> contact 7
-> contact 10
...
then fill the result in a php object and do like this:
foreach (cities as city)
{
// code here
}
Thanks in advance
Use GROUP_CONCAT
SELECT city, GROUP_CONCAT(name)
FROM contacts
GROUP BY city
You just need to GROUP BY city column and use GROUP_CONCAT function in SELECT to get comma seprated list of names for each city.
SELECT city, GROUP_CONCAT(name) AS name
FROM contacts
GROUP BY city;

MySQL: Get results given a condition

I have a table that looks like this:
target_id || country_code
5-----------||-------US----
5-----------||-------CA---
2----------||-------FR----
3----------||-------SP----
3----------||-------FR----
And another table that looks like this:
target_id || region_name
5-----------||---North America
2-----------||-----France------
3-----------||-----Some Europe
As you can see, table 2 contains locations and target_ids, while table 1 contains where these locations are targeted. In the case of North America, it is targeted to 5, which belongs to Canada and US. France, on the other hand has a target_id of 2, and Some Europe a target_id of 3, which contains France again and Spain.
What I would like to do via MySQL, is to get a table of target_id, country_code, country_name but only for countries. This means, only to the target_ids of table 1 that are in only one row (for example, we know that FR is a country because number 2 is only in FR, and we know that 3 represents a region because it has both Spain and France associated). Is this possible to do via MySQL or will I need two queries and PHP in the middle?
Thanks!
SELECT t1.target_id, t1.country_code, t2.region_name
FROM table1 t1
JOIN table t2
ON t1.target_id = t2.target_id
WHERE (SELECT COUNT(*) FROM table1 t3 WHERE t3.target_id = t1.target_id) = 1
table1 is the one with the country codes, table2 is the one with the the region names.