Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用SQL进行搜索_Php_Sql_Select_Search - Fatal编程技术网

Php 用SQL进行搜索

Php 用SQL进行搜索,php,sql,select,search,Php,Sql,Select,Search,我想使用PHP和SQL进行良好的搜索。我正在考虑使用类似SQL的运算符(例如:SELECT*FROM客户,其国家/地区为“%land%”),但我希望显示的第一个元素是以“land”开头的元素。我该怎么做?我不想对数据库执行多次查询,因为我认为这是个坏主意(但我可能错了)。我建议不要使用LIKE来实现搜索,它有很多限制。但如果您的用例很简单,那么您可以: SELECT 1 as orderby, * FROM Customers WHERE Country LIKE 'land%' union a

我想使用PHP和SQL进行良好的搜索。我正在考虑使用类似SQL的运算符(例如:
SELECT*FROM客户,其国家/地区为“%land%”
),但我希望显示的第一个元素是以“land”开头的元素。我该怎么做?我不想对数据库执行多次查询,因为我认为这是个坏主意(但我可能错了)。

我建议不要使用LIKE来实现搜索,它有很多限制。但如果您的用例很简单,那么您可以:

SELECT 1 as orderby, *
FROM Customers
WHERE Country LIKE 'land%'
union all
SELECT 2, *
FROM Customers
WHERE Country LIKE '_%land%'
order by orderby

这种方法允许您创建多个级别的排序

我建议不要使用LIKE来实现搜索,它有很多限制。但如果您的用例很简单,那么您可以:

SELECT 1 as orderby, *
FROM Customers
WHERE Country LIKE 'land%'
union all
SELECT 2, *
FROM Customers
WHERE Country LIKE '_%land%'
order by orderby

此方法允许您创建多个级别的排序

这将首先返回以
land
开头的任何内容,然后返回所有其他内容。 您需要特别列出您的字段

SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE 'land%'
UNION ALL
SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE '%land%'
AND country NOT LIKE 'land%'
或者,如果您有
customerid
字段,则在查询的第二部分使用子查询

SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE 'land%'
UNION ALL
SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE '%land%'
AND customerid NOT IN (SELECT customerid
                       FROM customers
                       WHERE country LIKE 'land%')

这将首先返回以
land
开头的任何内容,然后返回所有其他内容。 您需要特别列出您的字段

SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE 'land%'
UNION ALL
SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE '%land%'
AND country NOT LIKE 'land%'
或者,如果您有
customerid
字段,则在查询的第二部分使用子查询

SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE 'land%'
UNION ALL
SELECT field1, field2, field3, etc
FROM customers
WHERE country LIKE '%land%'
AND customerid NOT IN (SELECT customerid
                       FROM customers
                       WHERE country LIKE 'land%')
操作员就是您要查找的:

SELECT * FROM Customers 
WHERE Country LIKE 'land%' 
UNION 
SELECT * FROM Customers 
WHERE Country LIKE '%land%';
操作员就是您要查找的:

SELECT * FROM Customers 
WHERE Country LIKE 'land%' 
UNION 
SELECT * FROM Customers 
WHERE Country LIKE '%land%';

您可以根据国家/地区是否以
土地开始下单。我猜您正在使用的MySQL有一个很好的特性,当在数字上下文中引用时,它可以将真/假转换为1/0,所以这很容易做到:

SELECT   * 
FROM     Customers 
WHERE    Country LIKE '%land%'
ORDER BY Country LIKE 'land%' DESC
其他RDBMS可能没有这样的布尔处理,但您始终可以使用
case
表达式自己实现它:

SELECT   * 
FROM     Customers 
WHERE    Country LIKE '%land%'
ORDER BY CASE WHEN Country LIKE 'land%' THEN 1 ELSE 0 END DESC

您可以根据国家/地区是否以
土地开始下单。我猜您正在使用的MySQL有一个很好的特性,当在数字上下文中引用时,它可以将真/假转换为1/0,所以这很容易做到:

SELECT   * 
FROM     Customers 
WHERE    Country LIKE '%land%'
ORDER BY Country LIKE 'land%' DESC
其他RDBMS可能没有这样的布尔处理,但您始终可以使用
case
表达式自己实现它:

SELECT   * 
FROM     Customers 
WHERE    Country LIKE '%land%'
ORDER BY CASE WHEN Country LIKE 'land%' THEN 1 ELSE 0 END DESC

像'land%'
就是这样。是的,我知道,但我还想展示其他解决方案,因此我以那种方式使用LIKE。你需要澄清你的意思。如果您想要以
land
开头的,请按@Fred ii-建议使用。如果你想要其他结果,请解释更多。现在等待“它”。。。答案马上就来;-)编辑:啊,我很有洞察力。你用的是哪种SQL语言?MySQL、PostGreSQL、MS SQL Server、Oracle等?
比如“land%”
就是这样。是的,我知道,但我还想展示其他解决方案,因此我以那种方式使用LIKE。你需要澄清你的意思。如果您想要以
land
开头的,请按@Fred ii-建议使用。如果你想要其他结果,请解释更多。现在等待“它”。。。答案马上就来;-)编辑:啊,我很有洞察力。你用的是哪种SQL语言?MySQL、PostGreSQL、MS SQL Server、Oracle等?或者,根据方言,类似于按strpos('land',Country)
?@MatBailie的东西也可以。对于长字符串,类
的方法应该表现得更好一些,因为我们并不真正关心类
strpos
的结果,不管它是否为1。或者,根据方言,类
orderbystrpos('land',Country)
?@matbaile是的,这也会起作用。对于长字符串,
LIKE
方法的性能应该更好一些,因为我们并不真正关心
strpos
之类的结果,只关心它是否为1。