简体   繁体   English

如何在Codeigniter的SQL中为一个查询设置不同的限制不同列?

[英]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 如果有人搜索让我说“ abcd”,我需要什么,我希望这样显示结果

5 address which matches like "abcd" 5个匹配“ abcd”的地址

and 1 each from city, zipcode, state & country 以及城市,邮政编码,州和国家/地区各1个

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. 所以基本上我想在一个查询中将地址限制为5个地址,如果可用,则限制1个城市,如果可用,则限制1个邮政编码,如果可用,则限制1个州,如果可用,则限制1个国家。

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. 这个解决方案也是可行的,我通过keyup上的ajax显示结果,所以我担心运行多个查询会影响结果的加载时间。

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 我还尝试了一种使用UNION Operator的解决方案,它确实给了我所需的结果,但没有给出标识符以查看它是城市,州还是邮政编码

  $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. 并将一组子查询用于单独的LIMIT子句。

This is a working fiddle. 这是一个工作的小提琴。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将结果SQL不同查询设置为一个或不同变量? - How to set result SQL distinct query to one or different variables? 如何搜索3个不同的表并在一个查询中输出结果? - How can I search 3 different tables and output the results in one query? 如何在一个MySQL查询中从不同的表中进行选择? - How can I select from different tables in one MySQL query? 如何使用单个查询执行多个算术运算并将答案存储在同一表的两个不同列中 - How can I do more than one arithmetic operation using a single query and store the answer in two different columns on the same table 如何使用 SQL 将不同的数据库表合并为一个表? - How i can combine different database table into one using SQL? 如何设置列的值是使用同一表的列之一的SQL查询的结果 - How do I set the value of a column be a result of a SQL query using one of the columns of the same table 如何在Codeigniter中使用其他URL名称访问控制器 - How can i use a different url name to access a controller in codeigniter 在所有组合sql中用6个数字查询6个不同的列 - Query 6 different columns with 6 numbers in all combinations sql 如何将具有不同列数的行从一个表复制到另一表 - How can I copy rows from one to another table with a different number of columns 如何为用户和管理员设置不同的会话 | 代码点火器 - how to set different sessions for user and admin | CodeIgniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM