喜欢的问题

时间:2017-05-06 04:02:04

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

我有两张桌子......

 @IBAction func submitText(_ sender: Any) {
      if let text = textField.text {

            if let number = Int(text){

            arrayOfInt.append(number)

            }else {

             print("Please enter number")

           }

       }
 }

在以下查询中,当名称为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

2 个答案:

答案 0 :(得分:0)

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

package Routes;

import org.jooby.mvc.Path;

public class UserRoutes extends BaseRoutes {

  {

    get("/users", (req, resp) -> {
        resp.send("Uses index");
    });

    get("/user/:id", (req, resp) -> {
        resp.send("Single user page");
    });
  }

}

答案 1 :(得分:0)

更改您的where子句。使用与case表达式相同的条件,该表达式返回'none',即 clientes.idcliente为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
相关问题