I got a table [country] as follow
id | country_name
-----+---------------
1 India
2 USA
3 Nepal
4 SriLanka
When I try querying as follows, it works as expected
select group_concat(country_name)
from country
where id in (1, 2)
I get the result as I want
India,USA
But when I try the query using this way, I get a different result
select GROUP_CONCAT(country_name)
from country
where id in (CONVERT(REPLACE(REPLACE('[1,2]','[',''),']', ''), CHARACTER));
The result I get is
India
Require help in this regard.
you can try this !
select GROUP_CONCAT(country_name) from country
where id in (CONVERT(varchar,REPLACE(REPLACE('[1,2]','[',''),']','')));
I am not sure why you use the second query
but you may try using json
select GROUP_CONCAT(country_name) from country where JSON_SEARCH(CAST('["1","2"]' AS JSON), "one", id )
Using json_search mysql will look id in your json array.
The problem is, it works with ["1","2"] and not [1,2]
At last i found the answer
select GROUP_CONCAT(country_name) from country where FIND_IN_SET(id,REPLACE(REPLACE('[1,2]','[',''),']',''));
Related
I've tried to search for a solution to this but have been unable to find one. I guess it's basic SQL, but I can't seem to figure it out.
I have a table called people, this table has several columns:
ID Firstname Lastname Birthdate
1 John Stevenson 1860-07-30
2 Eric Johnson 1918-08-25
3 Adam Efron 1914-02-02
4 Michael Gray 1870-07-18
Now I want to make a query that looks at the Birthdate column, finds the lowest value and returns the firstname of the person that has the lowest birthdate (is oldest).
Can someone guide me in the right direction?
THanks in advance!
Use order by like
select Firstname
from people
order by Birthdate
limit 1
This will account for 2 paople having the same birthdate, returning 2 rows.
select Firstname
from people
where birthdate = (select min(birthdate) from people);
Another way to do it efficiently would be this (sqlfiddle):
select p.*, min(p.birthdate) from people p;
NOTE: You will have 1 extra column in the output.
Please how can I get the number of times, a specific word occur in a table?
I have a table called result and a field called president.
Index - president
1 - James
2 - John
3 - John
4 - John
5 - James
6 - John
I tried using the following codes to get the number of times John occur in my table.
SELECT COUNT(`president`) FROM `result` WHERE `president`="John"
But, it's writing syntax error.
N.B: What I just need is the number of times John appeared which I expected to be four(4).
You don't need to use COUNT on a column. In your case, you want to get
the number of rows where the president is 'John'.
Use the following syntax:
SELECT COUNT(*) FROM `result` WHERE `president` = "John"
P.S. Don't call your table result. It is kind of incorrect in terms of naming and architecture in general. Call it PresidentsHistory or PresidentsList.
Syntax COUNT(`president`) is not correct.
SELECT COUNT(*) FROM `result` WHERE `president` = "John"
You can try this mate:
SELECT
president, COUNT(index) 'occured'
FROM
result
WHERE
president = 'John';
E: This one is specific for 'John' only.
SELECT
president, COUNT(index) 'occured'
FROM
result
GROUP BY
president;
E: To display the count for each result.president in your database. Cheers!
you can try the below query
SELECT COUNT(*) FROM `result` WHERE `president` LIKE "John"
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%'
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
I have the following table:
surveys
comp_id question
4 What is your name?
4 How are you?
4 Where do you live?
5 Who are you?
5 What is your birthday?
I need help writing a Query that gives me the following output:
comp_id my_questions
4 What is your name?How are you?Where do you live?
5 Who are you?What is your birthday?
Thanks,
You are looking for the GROUP_CONCAT() function. Use it like this:
SELECT comp_id, GROUP_CONCAT([DISTINCT] question [ORDER BY some_field ASC/DESC] [SEPARATOR '']) as my_questions
FROM surveys
GROUP BY comp_id
Note I have shown some some optional values to pass into GROUP_CONCAT in []. To get exact like you are showing just use GROUP_CONCAT(question SEPARATOR ''). The optional items let you look for distinct question values or order them by any field(s) (including question itself).