E
E
Evgeny Shevtsov2014-10-25 21:49:10
MySQL
Evgeny Shevtsov, 2014-10-25 21:49:10

How to search by end part of a key in sql?

There is a table (wp_postmeta), the structure is as follows:

id post_id   key            value
1    11    fromcity_1       Москва
2    11    fromdistrict_1   Садовое кольцо
3    11    fromcity_2       Воронеж
4    11    fromdistrict_1   Коминтерновский
5    13    fromcity_2       Воронеж
6    13    fromdistrict_1   Северный

I have this sql query
SELECT post_id
FROM wp_postmeta AS p
WHERE p.`key` LIKE 'fromcity_%' AND p.`value` = 'Москва'
    AND EXISTS (
        SELECT 1
        FROM wp_postmeta AS c
        WHERE c.post_id = p.post_id
            AND c.`key` LIKE `fromdistrict_%`
            AND c.`value` = 'Садовое кольцо'
    )

The problem is that I need the id fromcity_ and fromdistrict_ to be the same and there are only records with the same key id ,
for example
fromcity_1 and fromdistrict_1
fromcity_2 and fromdistrict_2
how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
svd71, 2014-10-25
@Rattlesneyk

there is a word like:

select * from mytable where myfield like '%SOMETHING'  -- ищет чтоб SOMETHING было в конце

select * from mytable where myfield like '%SOMETHING%'  -- ищет в нутри с любой позиции

select * from mytable where myfield like 'SOMETHING%'  -- ищет сначала.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question