【问题标题】:Problems with LIKELIKE 的问题
【发布时间】:2017-05-06 04:02:04
【问题描述】:

我有两张桌子...

CREATE TABLE IF NOT EXISTS `clientes` (
  `idcliente` INT(10) NOT NULL AUTO_INCREMENT,
  `nombre` VARCHAR(50) NOT NULL,
  `apellido` VARCHAR(50) NOT NULL,
  `domicilio` VARCHAR(50) NOT NULL,
  `telefono` VARCHAR(50) DEFAULT NULL,
  `movil` VARCHAR(50) DEFAULT NULL,
  `dni` VARCHAR(10) NOT NULL,
  `familiar` VARCHAR(50) NOT NULL,
  PRIMARY KEY (`idcliente`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS `compras` (
  `idcompra` INT(7) NOT NULL AUTO_INCREMENT,
  `idcliente` INT(7) NOT NULL,
  `observacion` text NOT NULL,
  `fecha_ingreso` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`idcompra`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

在以下查询中,当名称为 none 时,LIKE 不返回任何内容。如何将like 添加到CASE WHEN

SELECT compras.idcompra, 
     CASE WHEN clientes.idcliente is null 
          THEN 'none' 
     ELSE CONCAT(clientes.nombre, ',', clientes.apellido) END 
     AS nombre FROM compras 
left join clientes  on (compras.idcliente=clientes.idcliente) 
where CONCAT(clientes.nombre, ' ', clientes.apellido) LIKE '%none%' 
order by compras.idcompra asc

例如,当使用跟随查询时...

select compras.idcompra, case when clientes.idcliente is null then 'none' else CONCAT(clientes.nombre, ',', clientes.apellido) end as nombre from compras 
left join clientes  on (compras.idcliente=clientes.idcliente) 

...我得到了这些结果...

+----------+--------+
| idcompra | nombre |
+----------+--------+
| 1        | none   |
| 2        | none   |
| 3        | none   |
| 4        | juan   |
| 5        | pepe   |
+----------+--------+

但是,以下查询返回 0 行。 like 无法识别 none

select compras.idcompra, case when clientes.idcliente is null then 'none' else CONCAT(clientes.nombre, ',', clientes.apellido) end as nombre 
from compras left join clientes  on (compras.idcliente=clientes.idcliente) 
where CONCAT(clientes.nombre, ' ', clientes.apellido) LIKE '%none%' 
order by compras.idcompra asc

¡¡我要退货!!

+----------+--------+
| idcompra | nombre |
+----------+--------+
| 1        | none   |
| 2        | none   |
| 3        | none   |
+----------+--------+

【问题讨论】:

  • 你的问题不清楚。您是否只想将like 条件从where 添加到case?如果不是,他们会给出一些示例数据和预期输出
  • 对不起,我不会说英语,我想输入搜索值 $ _POST [search] (none),但是当我输入 'none' 时,它什么也不返回。
  • 您能提供一些示例数据吗?以及基于此的预期输出。所以我们可以看到哪里出了问题。
  • @Utsav 编辑主帖。抬头

标签: mysql sql left-join case sql-like


【解决方案1】:

将以下内容添加到您的查询中:

and '[search term]' != 'none'

【讨论】:

  • 我需要写'none' ($_post[search]) 来返回记录¡我需要你返回这个!! 1-无 2-无 3-无
【解决方案2】:

更改您的 where 子句。使用与返回 'none' 的 case 表达式相同的条件,即 clientes.idcliente is null

SELECT compras.idcompra, 
     CASE WHEN clientes.idcliente is null 
          THEN 'none' 
     ELSE CONCAT(clientes.nombre, ',', clientes.apellido) END AS nombre
FROM compras 
left join clientes  on (compras.idcliente=clientes.idcliente) 
where clientes.idcliente IS NULL
order by compras.idcompra asc

另一种方法是形成原始查询的派生表,然后过滤案例表达式的结果,但我相信这会比上面的建议效率低。

SELECT idcompra, nombre 
FROM (
        SELECT compras.idcompra, 
             CASE WHEN clientes.idcliente is null 
                  THEN 'none' 
             ELSE CONCAT(clientes.nombre, ',', clientes.apellido) END AS nombre 
        FROM compras 
        left join clientes  on (compras.idcliente=clientes.idcliente) 
        ) D
WHERE nombre = 'none'
order by idcompra asc

【讨论】:

  • 我在 $_POST [搜索] 中输入了一个值。如果 '$ _POST [search] =' pe '' 将返回 5-pepe。如果是'ju',它将返回4-juan,如果是'non',则返回1-none,2-none,3-none。这就是为什么我需要使用 LIKE '% $ _ POST [search]%'。据了解
    where CONCAT(clientes.nombre, ' ', clientes.apellido) LIKE '%$_POST[search]%'
  • 您不能在同一查询的 where 子句中使用 case 表达式的结果。您必须使用“派生表”或使用 case 表达式的条件。我建议使用 case 表达式的条件更简单。 (nb. 我最初的回答不正确,因为我误读了问题,所以我很快就修复了它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
相关资源
最近更新 更多