php - 如果列位于另一列中,MYSQL 显示结果就像

标签 php mysql sql

我正在尝试让 MYSQL 查询仅显示与 2 个 WHERE 语句匹配的行。

例如

$result=mysql_query("
  SELECT *
  FROM tablename
  WHERE
    (column1 IN $phpimplodestring1) OR
    (column2 LIKE $phpimplodestring2)
  GROUP BY column2
  ORDER BY id /* another column */
  DESC LIMIT 5
")

到目前为止,我搜索的所有内容都不起作用,经过几周的测试,没有结果。 我尝试过使用mysqli,但在使用它时它似乎根本不起作用。

预先感谢您的帮助! :)

最佳答案

您的问题不是 WHERE 子句。 注意你的聚合,你按column2分组但在选择中声明*。 select 子句必须与聚合子句匹配,并且您甚至没有声明聚合函数。

下面的示例可以工作(取决于您想要什么)

SELECT column2, count(*) as QTD
  FROM tablename
  WHERE
    (column1 IN $phpimplodestring1) OR
    (column2 LIKE $phpimplodestring2)
  GROUP BY column2
  ORDER BY id /* another column */
  DESC LIMIT 5


SELECT *
  FROM tablename
  WHERE
    (column1 IN $phpimplodestring1) OR
    (column2 LIKE $phpimplodestring2)
  ORDER BY id /* another column */
  DESC LIMIT 5


SELECT column2, sum(anotherColumn)
  FROM tablename
  WHERE
    (column1 IN $phpimplodestring1) OR
    (column2 LIKE $phpimplodestring2)
  GROUP BY column2
  ORDER BY id /* another column */
  DESC LIMIT 5


SELECT distinct *
  FROM tablename
  WHERE
    (column1 IN $phpimplodestring1) OR
    (column2 LIKE $phpimplodestring2)
  ORDER BY id /* another column */
  DESC LIMIT 5

等等...

关于php - 如果列位于另一列中,MYSQL 显示结果就像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22092835/

相关文章:

php - 需要查询有时间冲突的用户,并返回那些时间冲突的用户

PHP Curl UTF-8 字符集

php - 将数据库结果存储到 PHP 中的变量

sql - 需要一些有关 Sql Server 和简单触发器的帮助

php - 如何在 laravel 中忽略某些有条件的字段的验证

php - flock 处理时会发生什么?

mysql - SQL : I need to explode a string by ">" and want to remove the last thread and Replace the (-) by space in CamelCase

php - while 循环内的查询仅显示 1 个结果

php - 从 wpdb 查询后动态设置选择列表的选定值

php - SQL PHP 检索特定商店的总金额