按顺序匹配数据

时间:2015-08-13 08:59:30

标签: mysql sql sql-order-by

我正在尝试根据用户搜索从多个表中获取数据。 但它根据我的要求不起作用。

我想从中获取记录的表:

  1. 国家
  2. 假设用户输入'a',那么我想首先从街道表中搜索,之后我想查看City表,最后我想查看状态表。

    尝试:

    我正在使用此查询来获取记录:

    SELECT streetObj.street_id AS street_id, CONCAT( streetObj.name,  ' ,    ', cityObj.name,  ' , ', stateObj.name ) namet 
        FROM street streetObj
     LEFT JOIN city cityObj ON streetObj.city_id = cityObj.city_id
        LEFT JOIN state stateObj ON stateObj.state_id = cityObj.state_id
            WHERE (
            streetObj.name LIKE  'a%'
            OR cityObj.name LIKE  'a%'
            OR stateObj.name LIKE  'a%'
           )
           LIMIT 10
    

    但是这个查询首先从州表中搜索状态表,然后从街道表中搜索。

    如何更改序列以查看表格?

    说明:

    街道:

    Ram darbar
    chawala street
    palam street
    

    城市:

     Kota
     Ahemdabad
     Jaipur
    

    国:

     Rajasthan
     Chennai
     Gujrat
    

    所以当用户输入'R'或'r'时,我希望看到这样:

    Ram darbar,Jaipur,Rajasthan
    

    但是我的查询给了我这样的结果:

    palam street,Jaipur,Rajasthan
    

    这也没有意义。

3 个答案:

答案 0 :(得分:0)

您可能希望将条件从WHERE移至JOIN ON子句,如

SELECT streetObj.street_id AS street_id,
       CONCAT(streetObj.name, ' ,    ', cityObj.name, ' , ', stateObj.name ) namet 
FROM street streetObj
  LEFT JOIN city cityObj ON streetObj.city_id = cityObj.city_id 
  LEFT JOIN state stateObj ON stateObj.state_id = cityObj.state_id 
  WHERE streetObj.name LIKE 'a%';

根据您最近的帖子编辑,您实际上并不需要以下条件,只应在street表中搜索。您应该根据JOIN

匹配的记录来获取数据
    OR cityObj.name LIKE  'a%'
    OR stateObj.name LIKE  'a%'

答案 1 :(得分:0)

SQL Fiddle

数据库架构

create table streettbl
(street_id varchar(20),
name varchar(30),
city_id varchar(20));

create table citytbl
(city_id varchar(20),
name varchar(30),
state_id varchar(30));

create table statetbl
(state_id varchar(20),
name varchar(30));

insert into streettbl values('street2','palam street','city1');
insert into streettbl values('street1','ram darbar','city1');
insert into citytbl values('city1','jaipur','state1');
insert into statetbl values('state1','rajasthan');

回答选择查询

 SELECT streetObj.street_id AS street_id, 
CONCAT( streetObj.name,  ' ,    ', cityObj.name,  ' , ', stateObj.name ) namet,
case when streetObj.name like 'r%' then 1
     when cityObj.name like 'r%' then 2
     when stateObj.name like 'r%' then 3
     else 0 end orderby
    FROM streettbl streetObj
 LEFT JOIN citytbl cityObj ON (streetObj.city_id = cityObj.city_id)
    LEFT JOIN statetbl stateObj ON (stateObj.state_id = cityObj.state_id)
        WHERE (
        streetObj.name LIKE  'r%'
        OR cityObj.name LIKE  'r%'
        OR stateObj.name LIKE  'r%'
       )
order by orderby

<强>输出

| street_id |                                namet | orderby |
|-----------|--------------------------------------|---------|
|   street1 |   ram darbar ,    jaipur , rajasthan |       1 |
|   street2 | palam street ,    jaipur , rajasthan |       3 |

答案 2 :(得分:0)

你可以试试 -

SELECT * FROM street AS stt
LEFT JOIN city AS ct ON stt.city_id=ct.id 
LEFT JOIN state AS st ON ct.state_id=st.id 
WHERE 
IF(
(SELECT COUNT(*) FROM street WHERE NAME LIKE 'a%')>0,stt.name LIKE 'a%', 
IF(
(SELECT COUNT(*) FROM city WHERE NAME LIKE 'a%')>0, ct.name LIKE 'a%', 
st.name LIKE 'a%'
)
) LIMIT 10;