SQL query in table wp_postmeta

0

How do I do a SQL query on a table that has the following fields:

SELECT meta_key,meta_value from wp_postmeta where meta_key = 'cidade' and meta_key = 'bairro'

the fields of the table wp_postmeta field meta_key = 'City' and meta_value = 'Curitiba' and meta_key = 'neighborhood' and meta_value = 'Center' is the following I want to make a select when I select a city in the other select it show only the neighborhoods related to the city that was selected so I want to know if you have how to do this, if you have how do I make this relationship in sql?

    
asked by anonymous 13.05.2015 / 15:53

3 answers

1

Your query appears to be correct, but be aware of the base name. In Wordpress (which I assume you are using), the table name is wp_postmeta and not wp_postpostmeta .

In a more comprehensive case, a simple select meta_key, meta_value from wp_postmeta returns all occurrences of the two fields

EDIT

Your query is not so correct, at least for me. Are you looking for all the cities And all the neighborhoods, or all the neighborhoods of a city? In WordPress logic, meta_key determines the name of the field, while meta_value , its value. Therefore, you would have meta_key = 'Cidade' and meta_value = 'Curitiba' , for example. IN THIS CASE (i.e., if your logic is this), to select all the cities in your relationship, your query would be:

SELECT meta_key, meta_value FROM wp_postmeta WHERE meta_key='Cidade'

So you would receive returns of type

meta_key | meta_value 
Cidade   | Curitiba
Cidade   | São Paulo

If, on the other hand, your meta_key is the NAME of the city, and the meta_value NAME

SELECT meta_key, meta_value FROM wp_postmeta WHERE meta_key='São Paulo'

Could return:

meta_key  | meta_value 
São Paulo | Morumbi
São Paulo | Jabaquara

If this last logic I represented is what you're following (i.e., meta_key = 'nome_da_cidade' ), and you want to list all neighborhoods in all cities (again, I'm assuming things here), do:

SELECT meta_key, meta_value FROM wp_postmeta ORDER BY meta_key

I drew my conclusions based on your question. See if I'm correct and let me know

    
13.05.2015 / 16:02
0
SELECT meta_key,meta_value FROM wp_postpostmeta WHERE meta_key = 'cidade' OR meta_key = 'bairro'
    
13.05.2015 / 16:03
0

The problem with your query is that you are using and between the two conditions. This way the bank will understand that it should return the meta_key and meta_value fields of all rows containing meta_key in the 'city' and 'neighborhood' value, but it does not have meta_key contain two different values on the same line.

To correct, you should use the or operator, since in your logic you want the database to return the columns of records that have meta_key in the value of 'city' OR 'neighborhood'.

SELECT meta_key,meta_value from wp_postpostmeta where (meta_key = 'cidade' OR meta_key = 'bairro')
    
13.05.2015 / 16:04