简体   繁体   English

mysql获取带有附加条件的不存在的结果

[英]mysql get non existing results with additional conditions

using mysql and php, 使用mysql和php,

I have two tables, I'm french so the table names may sound weird, but I would like to keep them as originals to avoid errors when testing… 我有两个表,我是法国人,所以表名听起来可能很奇怪,但我想将它们保留为原始名称,以避免测试时出错。

"arborescence" (it means categories) “树状”(表示类别)
"arborescence_domaine" (it means the subdomains for the categories) “ arborescence_domaine”(表示类别的子域)

The aim of this system is to be able to have great control 该系统的目的是要能够很好地控制
on the subdomains that depends on two things : the category and the zipcode. 取决于两个方面的子域:类别和邮政编码。
Just to explain you before I dive into the problem, 只是在我深入探讨问题之前向您解释,
for the same category, let's say "car", 对于同一个类别,我们说“汽车”,
for the zipcode 37000 the subdomain will be "supercars" 邮政编码37000的子域将是“超级跑车”
but for the zipcode 93000 the subdomain for the same category will be for example "the93supercars" 但对于邮政编码93000,同一类别的子域将是例如“ the93supercars”

That's why the "arborescence_domaine" contains the following fields : 因此,“ arborescence_domaine”包含以下字段:
id - arborescence_id - DEP - domaine - statut id-arborescence_id-DEP-域-状态

where "DEP" is the zipcode and "domaine" the subdomain and "statut" is a bool that decides if the subdomain is activated or not. 其中“ DEP”是邮政编码,“ domaine”是子域,“ statut”是布尔值,用于确定是否激活了子域。

Now, the fun part begins … 现在,有趣的部分开始了……

What I would like to do is do it within 1 single sql query if possible. 我想做的是如果可能的话,在1个单个sql查询中执行此操作。 (otherwise, I know thousands of php methods to achieve the same goal). (否则,我知道成千上万个实现相同目标的php方法)。

What I want now is get all the subdomains from a given array of category and a given zipcode. 我现在想要的是从给定的类别数组和给定的邮政编码中获取所有子域。 If the subdomain is not set, it doesn't exist in the "arborescence_domaine" table. 如果未设置子域,则该子域在“ arborescence_domaine”表中不存在。

But I want to have a result like this one : 但我想要这样的结果:

cat_id domaine DEP cat_id域DEP
3 vehicule-occasion 37 3辆37
14 immobilier 37 14防盗37
20 www NULL 20 www空
26 services-divers 37 26服务潜水员37
28 www NULL 28 www空
37 www NULL 37 www空
43 www NULL 43 www空
3467 www NULL 3467 www空

That means that if the subdomain is not set, it may returns the default 'www' value. 这意味着,如果未设置子域,则它可能会返回默认的“ www”值。

Now without the zipcode I learned how to do it a faw hours ago : 现在没有邮政编码,我在一小时前就学会了如何做:

This is a working request (the one that displayed the above array) 这是一个工作请求(显示上述数组的请求)
SELECT a.id as cat_id, IFNULL(d.domaine, 'www') as domaine, d.DEP SELECT a.id作为cat_id,IFNULL(d.domaine,'www')作为domaine,d.DEP
FROM arborescence a 从树突
LEFT JOIN arborescence_domaine d on d.arborescence_id=a.id d.arborescence_id = a.id上的左联接arborescence_domaine d
WHERE a.id in(3,14,20,26,28,37,3467,43); a.id in(3,14,20,26,28,37,3467,43);

But now with the zipcode I don't get the expected result : 但是现在用邮政编码我没有得到预期的结果:

The following request : 以下要求:

SELECT a.id as cat_id, IFNULL(d.domaine, 'www') as domaine, d.DEP SELECT a.id作为cat_id,IFNULL(d.domaine,'www')作为domaine,d.DEP
FROM arborescence a 从树突
LEFT JOIN arborescence_domaine d on d.arborescence_id=a.id d.arborescence_id = a.id上的左联接arborescence_domaine d
WHERE a.id in(3,14,20,26,28,37,3467,43) AND d.DEP='37' ; 其中a.id in(3,14,20,26,28,37,3467,43) 和d.DEP = '37' ;

Give the following result : 给出以下结果:

cat_id domaine DEP cat_id域DEP
3 vehicule-occasion 37 3辆37
14 immobilier 37 14防盗37
26 services-divers 37 26服务潜水员37

Well, the "in database not existing subdomains" have gone away. 好吧,“在数据库中不存在子域”已经消失了。

Now the question is : Is it possible to have a result like the first result shown, but using a zipcode filtering ? 现在的问题是:是否有可能显示所示的第一个结果,但使用邮政编码过滤?

(I thought I got it, but then a new problem occurred... as always...) (我以为我知道了,但是随后出现了一个新问题……一如既往……)

The d.DEP should be in your ON clause: d.DEP应该在您的ON子句中:

SELECT a.id as cat_id, IFNULL(d.domaine, 'www') as domaine, d.DEP
FROM arborescence a
LEFT JOIN arborescence_domaine d 
    ON d.arborescence_id=a.id AND d.DEP='37'AND d.status
WHERE a.id in(3,14,20,26,28,37,3467,43);

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

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