简体   繁体   English

从php&mySQL中的select语句中选择

[英]select from a select statement in php & mySQL

I am trying to add filters to a DB search. 我正在尝试向数据库搜索添加过滤器。 I have a search that takes some text and tries to find items with that text in the title. 我有一个需要一些文本的搜索,并试图找到标题中带有该文本的项目。 I also have a price range filter. 我也有一个价格范围过滤器。 That code is below and works just fine 该代码在下面并且可以正常工作

 $sql = "SELECT * FROM items where title LIKE '%". $title ."%' AND price > '". $price1 ."' AND price < '".$price2."' Limit 70";

Now I am trying to more and more filters. 现在,我正在尝试越来越多的过滤器。 Is there a select from the above code's output? 上面的代码输出有选择吗? I don't want to just keep making a longer SELECT statement with tons of if statements. 我不想只用大量的if语句来制作更长的SELECT语句。 I'd prefer to take the output of the previous select and refine that with another select. 我希望采用先前选择的输出,并使用另一个选择进行优化。 Is this possible? 这可能吗?

EDIT 1 Context: 编辑1上下文:

Users are the ones entering the information. 用户是输入信息的人。 This is for searching the items on my site. 这是用于搜索我的站点上的项目。

There's no other useful way than adding lots of different conditions to your WHERE cause, if you use plain SQL. 如果您使用纯SQL,除了向您的WHERE原因添加许多不同的条件外,没有其他有用的方法。 It is possible to use several nasted SELECT statements in your query, but this makes your code neither any more readable nor faster. 可以在查询中使用多个插入式SELECT语句,但这使您的代码既不可读也不快捷。

A more elegant solution is the usage of query objects or another form of object-oriented query abstraction (eg ZendDB). 更为优雅的解决方案是使用查询对象或另一种形式的面向对象的查询抽象(例如ZendDB)。

You can use some of the mysql string functions like INSTR(), MATCH which will make your life a little easy and also help the readability of the code. 您可以使用一些MySQL字符串函数,例如INSTR(),MATCH,这将使您的生活更加轻松,并且还有助于提高代码的可读性。 You can also use REGEXP and NOT REGEXP for pattern matching . 您也可以使用REGEXP和NOT REGEXP进行模式匹配。 The list of string functions are here . 字符串函数列表在这里

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

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