M
M
mastangt2015-05-30 21:56:36
MySQL
mastangt, 2015-05-30 21:56:36

How to organize a selection in SQL?

Good evening.
I am currently working with the FIAS database.
When registering, a person enters his city and a list with matches is loaded from the database.
All data is combined in one table.
Everything is built in a hierarchical system - the city has a parentguid which points to the aoguid of the region, region, etc.
I decided to combine all the necessary data into one View in MySQL.

VIEW `ADDR_TEST` AS select `a`.`id` AS `id`,`a`.`AOGUID` AS `AOGUID`,
`a`.`AOLEVEL` AS `AOLEVEL`,concat(`a`.`SHORTNAME`,',',`a`.`FORMALNAME`,' ( ',`b`.`FORMALNAME`,' ',`b`.`SHORTNAME`,' )') AS `NAME`
 from (`addr_table` `a` join `addr_table` `b`) 
where ((`a`.`AOLEVEL` in (4,6)) and (`b`.`AOLEVEL` = '3') and (`a`.`PARENTGUID` = `b`.`AOGUID`) and (`a`.`LIVESTATUS` = 1) and (`b`.`LIVESTATUS` = 1) and (`a`.`ACTSTATUS` = 1) and (`b`.`ACTSTATUS` = 1) and (`a`.`CURRSTATUS` = 0) and (`b`.`CURRSTATUS` = 0)) 
order by `a`.`AOLEVEL`

4 columns are obtained in the view - id, AOGUID(unique identifier of the city) , AOLEVEL(level (city->region->region)) , NAME (Normalized view of the settlement (Noginsk (Moscow region))
NAME -
concat(`a`.`SHORTNAME`,',',`a`.`FORMALNAME`,' ( ',`b`.`FORMALNAME`,' ',`b`.`SHORTNAME`,' )') AS `NAME`

The condition is the same. The point is (`a`.`PARENTGUID` = `b`.`AOGUID`)
that not all cities have a PARENTGUID and you need to somehow set a condition so that all cities are included in the view, i.e. you need to somehow make a double condition (If parentguid exists, then we leave everything as it is now, otherwise we just display the name of the city)
Actually, we need help with how to implement this.
Please help :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2015-05-30
@mastangt

LEFT JOIN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question