简体   繁体   English

订购 SQL 按结果数量查询

[英]Order a SQL Query by number of results

I want to do the following:我想做以下事情:

$searchParams is an array, which contains some strings (dynamicly generated). $searchParams 是一个数组,其中包含一些字符串(动态生成)。

Now the Statement would be something like this:现在声明将是这样的:

SELECT manufacturer FROM shop_articles WHERE manufacturer LIKE '".$searchParams."%'

But what I want is the result with the most matches.但我想要的是匹配最多的结果。 Can I code that in one statement?我可以在一个语句中编码吗? So it would be something like所以它会像

ORDER BY MATCHES DESC

How do I do that?我怎么做?

SELECT COUNT(*) AS matches, manufacturer FROM shop_articles WHERE manufacturer LIKE '".$searchParams."%' GROUP BY(manufaturer) ORDER BY matches ASC

Actually you have to measure matching (likeness) between manufacturer and $searchParam.实际上,您必须测量制造商和 $searchParam 之间的匹配(相似度)。 Unfortunatelly LIKE does not provide such functionality.不幸的是 LIKE 不提供这样的功能。 You may use Lavenshtein distances.您可以使用 Lavenshtein 距离。 See this post - Implementation of Levenshtein distance for mysql/fuzzy search?看到这篇文章 - mysql/模糊搜索的 Levenshtein 距离的实现?

For each param in your searchParam array you have to make a like clause对于 searchParam 数组中的每个参数,您必须创建一个 like 子句

    SELECT Manufacturer, COUNT(*) AS Matches FROM
    FROM shop_articles WHERE (
manufacturer LIKE '".$searchParams[0]."%' OR
manufacturer LIKE '".$searchParams[1]."%' OR 
...
manufacturer LIKE '".$searchParams[n]."%' OR )
    GROUP BY Manufacturer
    ORDER BY Matches

SELECT manufacturer, COUNT(manufacturer) FROM shop_articles WHERE manufacturer LIKE '".$searchParams."' GROUP BY manufacturer ORDER BY COUNT(manufacturer) DESC SELECT 制造商,COUNT(manufacturer) FROM shop_articles WHERE Manufacturer LIKE '".$searchParams."' GROUP BY Manufacturer ORDER BY COUNT(manufacturer) DESC

No, you cannot submit an array to the LIKE operator.不,您不能将数组提交给 LIKE 运算符。 It is more tedious than that.比这更乏味。 :-) :-)

When your data are not yet properly regularized, so that you have variants of the manufacturer name in the PRODUCTS table (eg "HP", "Hewlett Packard") rather than an integer ManufacturerID, you have to go through the grunt work of reducing those variants to a single entity.如果您的数据尚未正确规范化,因此您在 PRODUCTS 表中有制造商名称的变体(例如“HP”、“Hewlett Packard”)而不是 integer 制造商 ID,您必须通过减少那些繁重的工作来 go单个实体的变体。

A typical approach for doing that (quite unavoidable) work is to create a Manufacturers table like this:完成这项工作(非常不可避免)的一种典型方法是创建一个制造商表,如下所示:

  Table: MANUFACTURER

                   manufacturerid  INTEGER primary key
                   manufacturername  varchar
                   primarymanufacturerid  INTEGER  FOREIGN KEY REFERENCES MANUFACTURER(manufacturerid)

The last column allows you to associate a variant name (eg "HP") with the row where the main manufacturer record is stored (eg "Hewlett Packard").最后一列允许您将变体名称(例如“HP”)与存储主要制造商记录的行(例如“Hewlett Packard”)相关联。

                   124,   Hewlett Packard,    124
                   367,   HP,                 124

The row where primarymanufacturerid = manufacturerid is the the main entity. primarymanufacturid = Manufacturerid 的行是主要实体。

Then you could do this during an interim cleanup phase when you have not yet added a manufacturerid to the PRODUCTS table but it still has the name:然后,您可以在尚未将制造商 ID 添加到 PRODUCTS 表但它仍然具有名称的临时清理阶段执行此操作:

                 select * from products 
                 where manufacturer in
                 (
                   select manufacturername from manufacturer
                   where primarymanufacturerid = 
                    (
                     select primarymanufacturerid from manufacturer
                     where manufacturername = 'Hewlett Packard'
                     )
                 )

PS With a database engine that had support for functions and stored procedures, you could write your own function that accepted a delimited string of name variations, built a dynamic SQL statement, possibly using a temporary table to store the variant names one name per row, and returned a count of the matches. PS 使用支持函数和存储过程的数据库引擎,您可以编写自己的 function 接受分隔的名称变体字符串,构建动态 SQL 语句,可能使用临时表来存储变体名称每行一个名称,并返回匹配的计数。 This would be a resource-intensive approach recommended only to assist in the clean-up phase -- not something I'd put into production for end-users to consume as their daily bread.这将是一种资源密集型方法,仅推荐用于协助清理阶段——而不是我投入生产供最终用户作为日常面包消费的东西。

PPS And, of course, once you have your MANUFACTURER table properly created, with the primarymanufacturerid references completed, you could add a [manufacturerid] column to your PRODUCTS table and update it accordingly, and then dispense with all of this roundabout stuff. PPS 当然,一旦您正确创建了 MANUFACTURER 表,并完成了 primarymanufacturerid 引用,您可以将 [manufacturerid] 列添加到 PRODUCTS 表并相应地更新它,然后省去所有这些迂回的东西。

$sql = 'SELECT manufacturer FROM shop_articles WHERE 1=1'; $sql = '选择制造商 FROM shop_articles WHERE 1=1'; for($i=0;$i对于($i=0;$i

by this you can run query for $searchParams array dynamically通过这个你可以动态运行 $searchParams 数组的查询

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

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