我想做一个有多个输入参数的查询,其中一些可以为空。
此查询用于根据用户输入的值从数据库获取数据。
例如,他只能选择开始和结束日期以及小时,或者只选择产品等...
SELECT
cl.checklist_id AS id,
cl.checklist_indice AS indice,
cl.checklist_produit_id AS produitId,
cl.checklist_personne_id AS personneId,
cl.checklist_date AS date,
cl.checklist_heure AS heure,
cl.checklist_machine_id AS machineID
FROM
checklist cl,
personne p,
produit pr
WHERE cl.checklist_id IN (SELECT
cl.checklist_id
FROM
checklist cl
WHERE
:heureStart <= cl.checklist_heure
AND cl.checklist_heure <= :heureEnd
AND cl.checklist_id IN (SELECT
cl.checklist_id
FROM
checklist cl
WHERE
:dateStart <= cl.checklist_date
AND cl.checklist_date <= :dateEnd
AND cl.checklist_id IN (SELECT
cl.checklist_id
FROM
checklist cl
WHERE
cl.checklist_produit_id = :produitId
AND cl.checklist_id IN (SELECT
cl.checklist_id
FROM
checklist cl
WHERE
cl.checklist_machine_id = :machineId
AND cl.checklist_id IN (SELECT
cl.checklist_id
FROM
checklist cl
WHERE
cl.checklist_personne_id = :personneId)))));
如果我希望此查询正常工作,我必须填写所有值才能获取数据。
如果值为空(例如列上的 SELECT *
),是否有办法从列中获取所有数据?我需要对每种可能性进行查询吗?
最佳答案
也许像这样的查询?您必须查询每种可能性,但不能像您那样在嵌套查询中...
SELECT cl.checklist_id AS id,
cl.checklist_indice AS indice,
cl.checklist_produit_id AS produitId,
cl.checklist_personne_id AS personneId,
cl.checklist_date AS date,
cl.checklist_heure AS heure,
cl.checklist_machine_id AS machineID
FROM checklist cl, personne p, produit pr
WHERE (:heureStart is null OR :heureStart <= cl.checklist_heure)
AND ((:dateStart is null AND :dateEnd is null) OR (:dateStart <= cl.checklist_date AND cl.checklist_date <= :dateEnd))
AND (:produitId is null OR cl.checklist_produit_id = :produitId)
AND (:machineId is null OR cl.checklist_machine_id = :machineId)
AND (:personneId is null OR cl.checklist_personne_id = personneId);
关于php - 选择与特定值匹配的所有列,该值可以为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29963520/