I am pretty new to SQL in general and want to 'modify' the following table (Tablename: translations / PK: id, language, key):
| id | language | key | value |
---------------------------------------------
| 1 | DE | #Keyboard | Tastatur |
| 1 | EN | #Keyboard | Keyboard |
| 1 | ES | #Keyboard | Teclado |
| 2 | DE | #Screen | Bildschirm |
| 2 | EN | #Screen | Screen |
| 3 | ES | #Equipment | Equipo |
I want to get a table like this:
| key | valueDE | valueEN |
--------------------------------------------
| #Keyboard | Tastatur | Keyboard |
| #Screen | Bildschirm | Screen |
| #Equipment | null | null |
So basically I want the translation of the values for the languages DE and EN next to each other in columns. Entries with no translation for DE and EN should have an empty field in the column valueDE and valueEN or if only one translation is available the other field in the column should be empty as shown in the table above.
Help would be appreciated a lot.
Here it is, your definition re-worded into SQL.
select key,
(select value from translations where language='DE' and key=t.key limit 1) "valueDE",
(select value from translations where language='EN' and key=t.key limit 1) "valueEN"
from (select distinct key from translations) t;
key
valueDE
valueEN
#Keyboard
Tastatur
Keyboard
#Equipment
#Screen
Bildschirm
Screen
Related
We have various tables pertaining to different entities where we would like to globalize the stored values. We do not know how to proceed technically anymore and are open to any form of help, hints or tips.
Language
ID | Culture | Description |
---+---------+-------------+
1 | EN | English |
2 | FR | French |
3 | ES | Spanish |
Job
ID | Description |
---+-------------+
1 | Doctor |
2 | Firefighter |
JobGlobalization
ID | JobID | Description | Culture |
---+-------+-------------+---------+
1 | 1 | Docteur | FR |
2 | 1 | Doctora | ES |
We attempted to use CROSS JOIN to obtain something of the following:
ID | Description | Culture |
---+-------------+---------+
1 | Doctor | EN |
1 | Doctor | FR |
1 | Doctor | ES |
2 | Firefighter | ES |
2 | Firefighter | ES |
2 | Firefighter | ES |
Query used:
SELECT Job.ID, Job.Description, Language.Culture
CROSS JOIN Language
ORDER BY Job.ID
We experienced with different joins on the child globalization table in order to correlate the entities together, however the results set kept multiplying itself in the wrong way.
We would like that for every parent entity, whether it has any related child entities, a row is selected for every culture in the Language table. The description column will default to the parent entity in the case where there are no associated records in the child table.
The resulting table should be as follows:
ID | Description | Culture |
---+-------------+---------+
1 | Doctor | EN |
1 | Docteur | FR |
1 | Doctora | ES |
2 | Firefighter | EN |
2 | Firefighter | FR |
2 | Firefighter | ES |
We had in mind a condition that would select the 'Description' column from the parent table 'Job' if there were no corresponding record for it in the child table.
e.g.
IIF(JobGlobalization.Description IS NOT NULL, JobGlobalization.Description, Job.Description)
We attempted to use CROSS JOIN to obtain something of the following:
This should produce the result set you describe:
SELECT j.ID, j.Description, l.Culture
FROM Job j CROSS JOIN
Language l
ORDER BY j.ID, l.Culture;
You can insert this into JobGlobalization (although you might want to truncate it first). Or you can use CREATE TABLE AS (or the equivalent for your database) to create JobGlobalization from scratch.
You would then need to update this table with the appropriate values for the culture.
I have a table that contains a set of values and a hierarchyid column. Looks something like this:
+-----+-------------+-----------+
| ID | HierarchyID | Name | HierarchyID.ToString() for clarity
+-----+-------------+-----------+
| 1 | 0x58 | Testing | /1/
| 2 | 0x5AC0 | TChild1 | /1/1
| 3 | 0x5AD6 | TChild1.1 | /1/1/1
| 4 | 0x5ADA | TChild1.2 | /1/1/2/
| 5 | 0x68 | Example | /2/
| 6 | 0x6AC0 | EChild1 | /2/1
| ... | ... | ... |
+-----+-------------+-----------+
However, we are introducing a new data set of that aligns side by side with the current tree and I'll need to shift all the values in my current tree down a level so it should look something like this now.
+-----+-------------+-----------+
| ID | HierarchyID | Name | HierarchyID.ToString() for clarity
+-----+-------------+-----------+
| | 0x58 | OldData | /1/
| 1 | 0x5AC0 | Testing | /1/1/
| 2 | 0x5AC6 | TChild1 | /1/1/1
| 3 | 0x5AD6B0 | TChild1.1 | /1/1/1/1
| 4 | 0x5AD6D0 | TChild1.2 | /1/1/1/2/
| 5 | 0x5B40 | Example | /1/2/
| 6 | 0x5B56 | EChild1 | /1/2/1
| 6 | 0x68 | NewData | /2
| 6 | 0x6AC0 | NChild1 | /2/1
| ... | ... | ... |
+-----+-------------+-----------+
Is there an easy way to update all of my hierarchyid values to shift them down a level or do I have to update each row one by one without overlapping values on updates?
Just from looking up the documentation.
There is an easy way to move a subtree.
Working with hierarchyid Data
Under Moving Subtree there is an example for an Employee hierarchy.
You will have to adjust to your table structure.
CREATE PROCEDURE MoveOrg(#oldMgr nvarchar(256), #newMgr nvarchar(256) )
AS
BEGIN
DECLARE #nold hierarchyid, #nnew hierarchyid
SELECT #nold = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = #oldMgr ;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
SELECT #nnew = OrgNode FROM HumanResources.EmployeeDemo WHERE LoginID = #newMgr ;
SELECT #nnew = #nnew.GetDescendant(max(OrgNode), NULL)
FROM HumanResources.EmployeeDemo WHERE OrgNode.GetAncestor(1)=#nnew ;
UPDATE HumanResources.EmployeeDemo
SET OrgNode = OrgNode.GetReparentedValue(#nold, #nnew)
WHERE OrgNode.IsDescendantOf(#nold) = 1 ;
COMMIT TRANSACTION
END ;
GO
I have these two tables that i need to join
fields_data fields
+------------+-----------+------+ +------+-------------+----------+
| relationid | fieldname | data | | name | displayname | position |
+------------+-----------+------+ +------+-------------+----------+
| 2 | ftp | test | | user | Username | top |
| 2 | other | 1234 | | pass | Password | top |
+------------+-----------+------+ | ftp | FTP | top |
| log | Log | top |
| txt | Text | mid |
+------+-------------+----------+
I want to get all the rows from the "fields" table if they have the position "top" AND if a row has a match on name = fieldname from fields_data it should also show the data. This is my join
SELECT
fd.`data`,
fd.`relationid`,
fd.`fieldname`,
f.`name`,
f.`displayname`
FROM `fields` AS f
LEFT OUTER JOIN `fields_data` AS fd
ON fd.`fieldname` = f.`name`
WHERE f.`position`='top' AND (fd.`relationid`='3' OR fd.`relationid` IS NULL)
My problem is that the above query only gives me this result:
+------+------------+-----------+------+-------------+
| data | relationid | fieldname | name | displayname |
+------+------------+-----------+------+-------------+
| NULL | NULL | NULL | user | Username |
| NULL | NULL | NULL | pass | Password |
| NULL | NULL | NULL | log | Log |
+------+------------+-----------+------+-------------+
The field called "ftp" is missing due to it having a relation to "2".. However i still want to display it as result but like the others with NULL in it. And if the SQL query had "fd.relationid='2'" instead of 3 it would give same result, but with the row containing ftp in name, holding data in the three fields.
I hope you get what i mean.. My english is not the best.. Heres the result i want:
with above query containing fd.`relationid`='3'
+------+------------+-----------+------+-------------+
| data | relationid | fieldname | name | displayname |
+------+------------+-----------+------+-------------+
| NULL | NULL | NULL | user | Username |
| NULL | NULL | NULL | pass | Password |
| NULL | NULL | NULL | ftp | FTP |
| NULL | NULL | NULL | log | Log |
+------+------------+-----------+------+-------------+
with above query containing fd.`relationid`='2'
+------+------------+-----------+------+-------------+
| data | relationid | fieldname | name | displayname |
+------+------------+-----------+------+-------------+
| NULL | NULL | NULL | user | Username |
| NULL | NULL | NULL | pass | Password |
| test | 2 | ftp | ftp | FTP |
| NULL | NULL | NULL | log | Log |
+------+------------+-----------+------+-------------+
You want to move the condition to the on clause:
SELECT fd.`data`, fd.`relationid`, fd.`fieldname`, f.`name`, f.`displayname`
FROM `fields` f LEFT OUTER JOIN
`fields_data` fd
ON fd.`fieldname` = f.`name` AND fd.`relationid` = '3'
WHERE f.`position`='top' ;
It is interesting that the semantics of your query and this query are different -- and you found the exact situation: when there is a match on another value, the where clause form filters out the row. This will still keep everything.
As a note, the following also does what you want:
SELECT fd.`data`, fd.`relationid`, fd.`fieldname`, f.`name`, f.`displayname`
FROM `fields` f LEFT OUTER JOIN
(SELECT fd.*
FROM `fields_data` fd
WHERE fd.`relationid` = '3'
) fd
ON fd.`fieldname` = f.`name`
WHERE f.`position` = 'top' ;
I wouldn't recommend writing the query this way, particularly in MySQL (because the subquery is materialized). However, understanding why your version is different from these versions (and why these are the same) is a big step forward in mastering outer joins.
SOLVED ! See the answer bellow !
Before I explain my problem I want to apologise for those who would feel this question is too long but I feel like I must give some details to make things the clearer possible. Though, the problem is simple to understand it is not that simple to me to implement.
I have 3 tables.
Hata and Icon contains images I want to link with Succes which contains texts
[Hata]
id, INTEGER, AUTO_INCREMENT, PRIMARY_KEY
hata Image
idLang, VARCHAR(5)
[Icon]
id, INTEGER, AUTO_INCREMENT, PRIMARY_KEY
icon, IMAGE
idPhrase, INTEGER
[Succes]
id, INTEGER, AUTO_INCREMENT, PRIMARY_KEY
idPhrase, INTEGER
titre, VARCHAR(25)
desc, VARCHAR(125)
idLang, VARCHAR(5)
Here is a sample showing how the Succes table looks like
+----+----------+-----------------+------------------+--------+
| id | idPhrase | titre | desc | idLang |
+----+----------+-----------------+------------------+--------+
| 1 | 1 | Hello | Desc in English | en-GB |
+----+----------+-----------------+------------------+--------+
| 2 | 1 | Salut | Desc in French | fr-FR |
+----+----------+-----------------+------------------+--------+
| 3 | 1 | 今日は | Desc in Japanese | ja-JP |
+----+----------+-----------------+------------------+--------+
| 4 | 2 | Goodbye | Desc in English | en-GB |
+----+----------+-----------------+------------------+--------+
| 5 | 2 | Au revoir | Desc in French | fr-FR |
+----+----------+-----------------+------------------+--------+
| 6 | 2 | またね | Desc in Japanese | ja-JP |
+----+----------+-----------------+------------------+--------+
| 7 | 3 | You're welcome | Desc in English | en-GB |
+----+----------+-----------------+------------------+--------+
| 8 | 3 | Je vous en prie | Desc in French | fr-FR |
+----+----------+-----------------+------------------+--------+
| 9 | 3 | どういたしまして | Desc in Japanese | ja-JP |
+----+----------+-----------------+------------------+--------+
...
The tables are now joined using this WHERE conditions
Icons.idPhrase = Succes.idPhrase AND Hata.idLang=Succes.idLang
Everything would be fine if there would be nothing specific in the Succes table.
In fact, for each Icon there are 3 sentences and the idPhrase links them but in the actual result set I somehow have redundancies.
Icon1|FlagIcon1|TitreLang1|DescLang1
Icon1|FlagIcon2|TitreLang2|DescLang2
Icon1|FlagIcon3|TitreLang3|DescLang3
Icon2|FlagIcon1|TitreLang1|DescLang1
Icon2|FlagIcon2|TitreLang2|DescLang2
Icon2|FlagIcon3|TitreLang3|DescLang3
...
What I'd like to achieve is the following (just the very first row):
Icon1|FlagIcon1|TitreLang1|DescLang1|FlagIcon2|TitreLang2|DescLang2|FlagIcon3|TitreLang3|DescLang
or
Icon1|FlagIcon1|FlagIcon2|FlagIcon3|TitreLang1|DescLang1|TitreLang2|DescLang2|TitreLang3|DescLang3
or even
Icon1|FlagIcon1|FlagIcon2|FlagIcon3|TitreLang1|TitreLang2|TitreLang3|DescLang1|DescLang2|DescLang3
In other words, it would be like I'd joined several queries together such as
SELECT icon FROM Icon
Joined with
SELECT Hata.hata AS fEN, Succes.titre AS tEN, Succes.desc AS dEN
FROM Hata, Succes
WHERE Hata.idLang=Succes.idLang AND Succes.idLang='en-GB'
Joined With
SELECT Hata.hata AS fFR, Succes.titre AS tFR, Succes.desc AS dFR
FROM Hata, Succes
WHERE Hata.idLang=Succes.idLang AND Succes.idLang='fr-FR'
And so on...
Just the problem of ensuring the links between tables (icon 1 with sentence 1)
Here's another sample on how it should (may) look like
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| icon | fEN | fFR | fJP | tEN | tFR | tJA | dEN | dFR | dJA |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| <img> | <img> | <img> | <img> | Hello | Salut | 今日は | Desc in English | Desc in French | Desc in Japanese |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| <img> | <img> | <img> | <img> | Goodbye | Au revoir | またね | Desc in English | Desc in French | Desc in Japanese |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
| <img> | <img> | <img> | <img> | You're welcome | Je vous en pries | どういたしまして | Desc in English | Desc in French | Desc in Japanese |
+-------+-------+-------+-------+----------------+------------------+------------+-----------------+----------------+------------------+
...
I've browsed for SQL reference to try many things but they don't seem to do what I expect (CONCATENATE, UNION, etc...)
I also tried the following query but it gives me an error message.
SELECT Icon.icon, Hata.hata AS fEN,Hata.hata AS fFR,Hata.hata AS fJA
,'FR'.'titre', 'FR'.'desc'
,'JA'.'titre', 'JA'.'desc'
,'UK'.'titre', 'UK'.'desc'
FROM Hata, Icon
LEFT JOIN Succes AS FR ON 'FR'.'idLang' = 'Hata'.'idLang' AND 'FR'.'idLang' = 'fr-FR'
LEFT JOIN Succes AS JA ON 'JA'.'idLang' = 'Hata'.'idLang' AND 'FR'.'idLang' = 'ja-JP'
LEFT JOIN Succes AS UK ON 'UK'.'idLang' = 'Hata'.'idLang' AND 'FR'.'idLang' = 'en-GB'
the message is
Statut SQL: HY000
Error Code: 1000
syntax error, unexpected $end, expecting BETWEEN or IN or SQL_TOKEN_LIKE
but it seems my syntax is good according to sample I've found even on StackOverflow.
I must also specify that I'm using OpenOffice Base and my purpose is publishing a document. Maybe there is something specific to OOo such as LEFT JOIN not implemented but the code get coloured so I think it should be fine.
Thank you for your availability and help.
I really don't get it.
I've tried with MySQL and it does something like an exclusive join
mysql> SELECT titre AS tfr, titre AS ten, titre AS tjp FROM data WHERE idlang=1
-> UNION
-> SELECT null,titre AS ten, null FROM data WHERE idlang=2
-> UNION
-> SELECT null, null, titre as tjp FROM data WHERE idlang=3;
+------------------+------------------+------------------+
| tfr | ten | tjp |
+------------------+------------------+------------------+
| Salut | Salut | Salut |
| Au revoir | Au revoir | Au revoir |
| Je vous en pries | Je vous en pries | Je vous en pries |
| NULL | Hello | NULL |
| NULL | Goodbye | NULL |
| NULL | You're Welcome | NULL |
| NULL | NULL | Konnichiha |
| NULL | NULL | Mata ne |
| NULL | NULL | Douitashimashite |
+------------------+------------------+------------------+
9 rows in set (0.00 sec)
If in the 1st SELECT I do titre AS tfr, null, null the column headers get to null.
mysql> SELECT titre AS tfr, titre AS ten, titre AS tjp FROM data WHERE idlang=1
-> UNION
-> SELECT null,titre AS ten, null FROM data WHERE idlang=2
-> UNION
-> SELECT null, null, titre as tjp FROM data WHERE idlang=3;
+------------------+------------------+------------------+
| tfr | NULL | NULL |
+------------------+------------------+------------------+
| Salut | NULL | NULL |
| Au revoir | NULL | NULL |
| Je vous en pries | NULL | NULL |
| NULL | Hello | NULL |
| NULL | Goodbye | NULL |
| NULL | You're Welcome | NULL |
| NULL | NULL | Konnichiha |
| NULL | NULL | Mata ne |
| NULL | NULL | Douitashimashite |
+------------------+------------------+------------------+
It still doesn't looks like the result I want.
I need to concentrate on that data table to get all in one line but I keep wondering how to achieve this. In principle it is very simple but I can't translate that in SQL.
mysql> DESCRIBE data;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_phrase | int(11) | YES | | NULL | |
| titre | varchar(20) | YES | | NULL | |
| desc | varchar(50) | YES | | NULL | |
| idicon | int(11) | YES | | NULL | |
| idlang | int(11) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
Actually idicon is redundant with id_phrase (don't really need it so pretend it does not exist).
Thank you.
HERE ARE SOME PROPOSITIONS IF YOU WOULD MEET A RESEMBLING PROBLEM.
NB: The column names and table names may differ from the original question but the problem is the same.
I've been asking this question on another forum and here are 2 queries I tested and can certify are working with MySQL 5.5
Query 1 :
SELECT id_phrase
, idicon
, max(case idlang when 1 then titre end) AS tfr
, max(case idlang when 1 then DESC end) AS dfr
, max(case idlang when 2 then titre end) AS ten
, max(case idlang when 2 then DESC end) AS den
, max(case idlang when 3 then titre end) AS tjp
, max(case idlang when 3 then DESC end) AS djp
FROM DATA
WHERE idlang IN (1, 2, 3)
GROUP BY id_phrase, idicon
ORDER BY id_phrase ASC
Query 2 :
SELECT t1.id_phrase, t1.idicon, t1.titre AS tfr, t1.descr AS dfr, t2.titre AS ten, t2.descr AS den, t3.titre AS tjp, t3.descr AS djp
FROM DATA AS t1
LEFT OUTER JOIN DATA AS t2
ON t1.id_phrase=t2.id_phrase
LEFT OUTER JOIN DATA AS t3
ON t1.id_phrase=t3.id_phrase
WHERE t1.idlang=1 AND t2.idlang=2 AND t3.idlang=3
You're welcome if these queries may help you.
Source (french)
Description
According to the explain command, there is a range that is causing a query to perform a full table scan (160k rows). How do I keep the range condition and reduce the scanning? I expect the culprit to be:
Y.YEAR BETWEEN 1900 AND 2009 AND
Code
Here is the code that has the range condition (the STATION_DISTRICT is likely superfluous).
SELECT
COUNT(1) as MEASUREMENTS,
AVG(D.AMOUNT) as AMOUNT,
Y.YEAR as YEAR,
MAKEDATE(Y.YEAR,1) as AMOUNT_DATE
FROM
CITY C,
STATION S,
STATION_DISTRICT SD,
YEAR_REF Y FORCE INDEX(YEAR_IDX),
MONTH_REF M,
DAILY D
WHERE
-- For a specific city ...
--
C.ID = 10663 AND
-- Find all the stations within a specific unit radius ...
--
6371.009 *
SQRT(
POW(RADIANS(C.LATITUDE_DECIMAL - S.LATITUDE_DECIMAL), 2) +
(COS(RADIANS(C.LATITUDE_DECIMAL + S.LATITUDE_DECIMAL) / 2) *
POW(RADIANS(C.LONGITUDE_DECIMAL - S.LONGITUDE_DECIMAL), 2)) ) <= 50 AND
-- Get the station district identification for the matching station.
--
S.STATION_DISTRICT_ID = SD.ID AND
-- Gather all known years for that station ...
--
Y.STATION_DISTRICT_ID = SD.ID AND
-- The data before 1900 is shaky; insufficient after 2009.
--
Y.YEAR BETWEEN 1900 AND 2009 AND
-- Filtered by all known months ...
--
M.YEAR_REF_ID = Y.ID AND
-- Whittled down by category ...
--
M.CATEGORY_ID = '003' AND
-- Into the valid daily climate data.
--
M.ID = D.MONTH_REF_ID AND
D.DAILY_FLAG_ID <> 'M'
GROUP BY
Y.YEAR
Update
The SQL is performing a full table scan, which results in MySQL performing a "copy to tmp table", as shown here:
+----+-------------+-------+--------+-----------------------------------+--------------+---------+-------------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-----------------------------------+--------------+---------+-------------------------------+--------+-------------+
| 1 | SIMPLE | C | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| 1 | SIMPLE | Y | range | YEAR_IDX | YEAR_IDX | 4 | NULL | 160422 | Using where |
| 1 | SIMPLE | SD | eq_ref | PRIMARY | PRIMARY | 4 | climate.Y.STATION_DISTRICT_ID | 1 | Using index |
| 1 | SIMPLE | S | eq_ref | PRIMARY | PRIMARY | 4 | climate.SD.ID | 1 | Using where |
| 1 | SIMPLE | M | ref | PRIMARY,YEAR_REF_IDX,CATEGORY_IDX | YEAR_REF_IDX | 8 | climate.Y.ID | 54 | Using where |
| 1 | SIMPLE | D | ref | INDEX | INDEX | 8 | climate.M.ID | 11 | Using where |
+----+-------------+-------+--------+-----------------------------------+--------------+---------+-------------------------------+--------+-------------+
Answer
After using the STRAIGHT_JOIN:
+----+-------------+-------+--------+-----------------------------------+---------------+---------+-------------------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-----------------------------------+---------------+---------+-------------------------------+------+---------------------------------+
| 1 | SIMPLE | C | const | PRIMARY | PRIMARY | 4 | const | 1 | Using temporary; Using filesort |
| 1 | SIMPLE | S | ALL | PRIMARY | NULL | NULL | NULL | 7795 | Using where |
| 1 | SIMPLE | SD | eq_ref | PRIMARY | PRIMARY | 4 | climate.S.STATION_DISTRICT_ID | 1 | Using index |
| 1 | SIMPLE | Y | ref | PRIMARY,STAT_YEAR_IDX | STAT_YEAR_IDX | 4 | climate.S.STATION_DISTRICT_ID | 1650 | Using where |
| 1 | SIMPLE | M | ref | PRIMARY,YEAR_REF_IDX,CATEGORY_IDX | YEAR_REF_IDX | 8 | climate.Y.ID | 54 | Using where |
| 1 | SIMPLE | D | ref | INDEX | INDEX | 8 | climate.M.ID | 11 | Using where |
+----+-------------+-------+--------+-----------------------------------+---------------+---------+-------------------------------+------+---------------------------------+
Related
http://dev.mysql.com/doc/refman/5.0/en/how-to-avoid-table-scan.html
http://dev.mysql.com/doc/refman/5.0/en/where-optimizations.html
Optimize SQL that uses between clause
Thank you!
ONE Request... It looks like you KNOW your data. Add the keyword "STRAIGHT_JOIN" and see the results...
SELECT STRAIGHT_JOIN ... the rest of your query...
Straight-join tells MySql to DO IT AS I HAVE LISTED. So, your CITY table is the first in the FROM list, thus indicating you expect that to be your primary... Additionally, your WHERE clause of the CITY is the immediate filter. With that being said, it will probably fly through the rest of the query...
Hope it helps... Its worked for me with gov't data of millions of records queried and joined to 10+ lookup tables where mySql was trying to think for me.
in order to do efficient between queries you are going to want a b tree index on your YEAR column. for example:
CREATE INDEX id_index USING BTREE ON YEAR_REF (YEAR);
BTREE indexes allow for efficient range queries, if this is in fact the root problem then having an index like this should get rid of the full table scan and have it only scan the part of the table that is in the range. read more about btrees on wikipedia
However, as with any optimisation advice, you should measure to make sure that you don't do more harm than good.
Can you change from searching within a radius to search in a bounding box?
You know the city so you can calculate a bounding box in your application.
Perhaps this
S.LATITUDE_DECIMAL >= latitude_lower and
S.LATITUDE_DECIMAL <= latitude_upper and
S.LONGITUDE_DECIMAL >= longitude_lower and
S.LONGITUDE_DECIMAL <= longitude_upper
could be a little faster?