Search for field contents contained in other records - mysql

I have a list table having over 200,000 rows with city column. Assume that it contains following data:
rowid city
1 Toronto
2 Milton
3 Hamilton
4 Delhi
5 New Delhi
6 Markham
I want to find all records where the city is contained in another row, e.g. Milton (row 2) is contained in Hamilton (row 3) and Delhi (row 4) is contained in New Delhi (row 5). I expect the following output:
rowid city rowid2 city2
2 Milton 3 Hamilton
4 Delhi 5 New Delhi
Can a single query get the output I am looking for?
Thanks.

Assume table name is 'cities'. Following SQL query should work:
select
c2.rowid, c2.city, c1.rowid, c1.city
from
cities c1
inner join
cities c2
on instr(c1.city,c2.city)
and c1.rowid != c2.rowid

Related

Fetch fixed set of duplicate records from table

I want to fetch records from a table that contains duplicate records. I want the output to be like only two duplicate records from each set of duplicate records in overall record output set.
example-
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Monesy
Russia
Bhumi
India
Peter
England
Bruice
England
Radhe
India
Output should have only two duplicate set of records from all duplicate of similar type as we can see in output below the country is repeating only two times and it took only first two counters of duplicate records in final record set -
Name
Country
John
India
Mark
India
Chris
Russia
Feggy
England
Rain
Russia
Peter
England
You can number the lines by the window and select only the first N.
Sorting should be chosen according to the business logic of the query.
For example:
;WITH numbered_name AS
(
SELECT *
, ROW_NUMBER() OVER (PARTITION BY t.Country ORDER BY t.Name) rn
FROM table t
)
SELECT Name
, Country
FROM numbered_name
WHERE rn <= 2

SQL Select, replace ID with table column from foreign key

Lets say I have two tables, and in this example, the fish table has Place as a foreign key. In a select statement, how can I get the average weight per place, but instead of showing the id, it shows either the name of the place or the country that has the same id.
Fish
ID Type Weigth Place
1 Cod 300 1
2 Pike 600 2
3 Pike 1000 2
4 Salmon 800 1
Place
ID Name Country
1 NY USA
2 London UK
3 Oslo Norway
I can only figure out how to get average weight per place and return the id, but not the name or country of the place. I think I need to use join of some sort, but cant figure out how.
Thanks in advance

Passing Multiple ids to single parameter in mysql

I have table emloyee with following data.
Id Name Country
1 John USA
2 Smith USA
3 Jack IND
4 Lory UK
5 Miller USA
I want to get result by calling stored procedure like this
call getEmployeeDetailsByCountry('IND,UK');
Result:
Id Name Country
3 Jack IND
4 Lory UK
My Procedure is
select * from employee
where if( (LOCATE(',','IND,USD')>0),
Country in (concat('\'',REPLACE('IND,USD', ',', '\',\''),'\''))
, Country in ('IND,USD'));
Here it replaces 'IND,USD' to 'IND','USD'.
But result is no rows.... Can anyone help me to find..... thank you
Use FIND_IN_SET instead of IN
SELECT *
FROM product
WHERE FIND_IN_SET(country, 'IND,USD');
Try this i think this will solve your problem
Why dont you use a query with a simple IN statement in the where clause?
SELECT * FROM employee WHERE Country IN ('IND', 'UK');

MySQL: show only a record in left join when is present a value or another in right table

I had a table with these entries:
id name id_language
1 San Paolo Stadium 2
1 Stadio San Paolo 1
1 Stade San Paolo 3
2 Castel Nuovo 1
2 Maschio Angioino 2
3 Abbazia di Sant'Antimo 1
4 Fontana di Trevi 1
4 Trevi Fountain 2
4 Trevi-Brunnen 3
First column belongs to a table called "Places", containing informations about a certain "place". Second and third column belong to another table called "Names", containing names of these places in various languages, identified by "id_language" value. These two tables are joined with a "one to many relationship", where one "Place" can have one ore more "names" in different languages.
I want to show with a query ONE AND ONLY of these names per place, with this criteria:
1) If exists a name of a place with a certain id_language given to the query, print ONLY that name;
2) Else, print the name of that place with another id_language.
Example: I want all the places having a name with id_language = 2. The places without a name with id_language = 2 will be printed with the name containing id_language = 1, like this:
id name id_language
1 San Paolo Stadium 2
2 Maschio Angioino 2
3 Abbazia di Sant'Antimo 1
4 Trevi Fountain 2
Original query:
SELECT places.id,
name_descriptions.name,
name_descriptions.id_language
FROM places
LEFT JOIN places_name_descriptions ON places.id = places_name_descriptions.id_places
LEFT JOIN name_descriptions ON places_name_descriptions.id_name_description = name_descriptions.id
ORDER BY places.id, name_descriptions.name ASC

MySQL table JOIN with LIKE without adding another ID column

I have two tables named "regions" and "city_list":
REGIONS TABLE:
r_id r_code r_name
1 REGION I Ilocos Region
2 REGION II Cagayan Valley
3 REGION III Central Luzon
4 REGION IV-A CALABARZON
5 REGION IV-B MIMAROPA
CITY_LIST TABLE:
city_id city_name city_region
32 Catbalogan REGION VIII (Eastern Visayas)
33 Cauayan REGION II (Cagayan Valley)
34 Cavite City REGION IV-A (CALABARZON)
35 Cebu City REGION VII (Central Visayas)
87 City of Naga REGION VII (Central Visayas)
I want to reference the regions table when a user selects a city name from the city_list table. For example:
User selects "Cauauyan" from a drop-down list, the server returns the region code which is the "r_code" column from the Regions table.
Can I use a JOIN statement with LIKE without adding a column for a region ID in the city_list table? Is it possible to get the value in city_region like "REGION II" without the "(Cagayan Valley)" and use that value to get r_id in the REGIONS table?
SELECT r.r_id
FROM REGIONS r
WHERE r.`r_code` IN(
SELECT LEFT(`CITY_LIST`.`city_region`,INSTR(`CITY_LIST`.`city_region`,"(")-1) AS Region_id FROM `CITY_LIST`);