Select location from status

Asked

Viewed 45 times

0

I have the following structure (fictitious)

Status table:

id | name | status 

Table location:

id | title | estado_id

In my front-end I have a navbar which will list the states and in each state the localities.

I’m using the following query

SELECT estado.name, localizacao.title
FROM estado
INNER JOIN localizacao ON estado.id = localizaçao.estado_id

It works cool, the problem is that it returns the state according to the amount of location.

Here is an example of the return

name  | title 
EUA   | cidade
EUA   | cidade
USA.  | cidade

I don’t want the USA repeat.

Solved temporarily using two separate queries.

  • Put an example, in table form, of how you want it to come back. I didn’t get it right.

  • I have a div of categories and in these categories are listed the information. In the query I showed it brings the repeated categories, I do not want you to repeat. It would be something like an array, $Category = array("categoria1"=>array("1","2")); this categorie1 cannot repeat.

  • 1

    In this example you gave, these two records with name=USA, the field title will have the same value in both records?

  • Only sql returns everything in table. The data you want is there. You need to loop them by php and turn them into the array structure you want. And this structure you use to generate html.

  • The title can be different. Name is the state and title the city. It would be like a group, if I have 40 cities in SP, I want to return the cities as a set of SP and not a city pro same state 40 times. The only logic I found was in separate query.

  • Either you query separately, or you do this with Join and then turn it into the structure you want. sql does not work with nested structures, only tabular data.

  • Utilize JSON from Mysql to return only the name and a JSON Array with the "Titles".

  • @bfvaretto exactly this, I feel like a gambiarra, the way is treat in PHP, I thought there was another way. Thanks.

  • In this query that showed, will always bring the repeated states, can each state can have several locations. Therefore, your problem is on the screen Sula. You will be required to have two queries. The first query will bring all the states, when selecting the state makes the query with the state ID returning the cities belonging to that state.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.