简体   繁体   中英

How can i set different limit different columns in one query in SQL, Codeigniter?

I have a table of data and i need the search results to show by column name

i have this table

id address        city      zipcode  state  country
1  12, street     arizona   1234     abcd   new island
2  abcd,stree     abcd      7895     abcd   us
3  new av         mumbai    6875     abcd   uk
4  abcd, street   test ci   5687     abcd   new island
5  6741, street   df df     12345    abcd   uae

What i need if someone searches for lets say "abcd" i want the results to be shown like this

5 address which matches like "abcd"

and 1 each from city, zipcode, state & country

so basically i want in one query to limit address to get 5 address, limit 1 city if available, limit 1 zipcode if available, limit 1 state if available, limit 1 country if available.

also the keyword should look for each column not for only address. i did achive this by using multiple queries

 $query = $this->db->query("SELECT city FROM ".$this->db->dbprefix('prison')." WHERE city like'%".$keyword."%' LIMIT 1");
    $query2 = $this->db->query("SELECT zipcode FROM ".$this->db->dbprefix('prison')." WHERE zipcode like'%".$keyword."%' LIMIT 1");
    $query3 = $this->db->query("SELECT state FROM ".$this->db->dbprefix('prison')." WHERE state like'%".$keyword."%' LIMIT 1");
    $query4 = $this->db->query("SELECT address FROM ".$this->db->dbprefix('prison')." WHERE address like'%".$keyword."%' LIMIT 10");
    $query5 = $this->db->query("SELECT country FROM ".$this->db->dbprefix('prison')." WHERE country like'%".$keyword."%' LIMIT 1");

    $result_array = [];
    $result_array['cities'] =  $query->row();
    $result_array['zipcode'] =  $query2->row();
    $result_array['state'] =  $query3->row();
    $result_array['address'] =  $query4->result();
    $result_array['country'] =  $query5->row();

    return $result_array;

is there anyway i can achieve this in one query? also is this solution feasible i am showing result via ajax on keyup so i am worried running multiple queries will affect the loading time of result.

i also tried one solution using UNION Operator it does give me results i need but does not give the identifier to see whether it is a city, state or zipcode

  $query = $this->db->query("SELECT prison_city FROM ".$this->db->dbprefix('prison')." WHERE prison_city like'%".$keyword."%' LIMIT 1 UNION SELECT prison_zipcode FROM ".$this->db->dbprefix('prison')." WHERE prison_zipcode like'%".$keyword."%' LIMIT 1 UNION SELECT prison_state FROM ".$this->db->dbprefix('prison')." WHERE prison_state like'%".$keyword."%' LIMIT 1 UNION SELECT prison_country FROM ".$this->db->dbprefix('prison')." WHERE prison_country like'%".$keyword."%' LIMIT 1 UNION SELECT prison_address FROM ".$this->db->dbprefix('prison')." WHERE prison_address like'%".$keyword."%' LIMIT 10");


    return $query->result();

it outputs this data

Array ( 
  [0] => stdClass Object ( [prison_city] => Susanville ) 
  [1] => stdClass Object ( [prison_city] => California ) 
  [2] => stdClass Object ( [prison_city] => 24900 CA-202 ) 
  [3] => stdClass Object ( [prison_city] => New York, America ) 
  [4] => stdClass Object ( [prison_city] => 1 Kings Way, Avenal, CA 93204, USA ) 
  [5] => stdClass Object ( [prison_city] => 14901 Central Ave ) 
  [6] => stdClass Object ( [prison_city] => 19025 Wiley's Well Road ) 
  [7] => stdClass Object ( [prison_city] => 23500 Kasson Rd ) 
  [8] => stdClass Object ( [prison_city] => 475-750 Rice Canyon Rd ) 
  [9] => stdClass Object ( [prison_city] => 19005 Wiley's Well Road ) 
)

it has two issues, first of all it does not give the identifier and second the limit which i set only for address it is applying to entire result.

you can use union with a custom value. For Example:-

    SELECT * FROM (SELECT prison_city , 1 AS type FROM ".$this->db->dbprefix('prison')." WHERE prison_city like'%".$keyword."%' LIMIT 1
   ) tb1  UNION (SELECT prison_zipcode ,2 FROM ".$this->db->dbprefix('prison')." WHERE prison_zipcode like'%".$keyword."%' LIMIT 1) 
    UNION (SELECT prison_state,3 FROM ".$this->db->dbprefix('prison')." WHERE prison_state like'%".$keyword."%' LIMIT 1) 
    UNION (SELECT prison_country,4 FROM ".$this->db->dbprefix('prison')." WHERE prison_country like'%".$keyword."%' LIMIT 1) 
    UNION (SELECT prison_address,5 FROM ".$this->db->dbprefix('prison')." WHERE prison_address like'%".$keyword."%' LIMIT 10) 

And use a set of sub queries for separate LIMIT clauses.

This is a working fiddle.

http://sqlfiddle.com/#!9/a6c585/74700

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM