简体   繁体   English

mysql union或仅加入一个查询

[英]mysql union or join just one query

I need to find a better way to get the discount for each article price in our web shop depending on which pricelist or pricelist/discount list a customer has. 我需要找到一种更好的方法来获取网上商店中每种商品价格的折扣,具体取决于客户拥有的价格表或价格表/折扣表。 I think this is possible to do in just one query instead of the 5 I have today, but I really do not know where to start. 我认为这仅用一个查询即可完成,而不是今天的5个查询,但我真的不知道从哪里开始。

All our customers have a pricelist and some have both pricelist and one extra discount list. 我们所有的客户都有一个价目表,有些既有价目表又有一个额外的折扣表。 Today we have about 25 different pricelists and about 100 extra discount lists. 今天,我们有大约25种不同的价目表和大约100种额外的折扣表。 All the pricelists are structured in the same manner; 所有价目表的结构均相同。 they have a price group and a discount in percent. 他们有一个价格组和一个百分比折扣。

For example pricelist 01 could look like 例如,价目表01可能看起来像

A 20 A 20

B 35 B 35

C 20 C 20

The extra discount list is structured in a different manner and can have a fixed price or percentage. 额外折扣清单的结构不同,可以有固定的价格或百分比。 It also has three different priority levels: discount based on the article code (has priority 1), based on category (has priority 2) and based on price group (has priority 3). 它还具有三个不同的优先级:基于商品代码(具有优先级1),基于类别(具有优先级2)和基于价格组(具有优先级3)的折扣。

Discount list 0013 could look like: 折扣清单0013可能类似于:

In the article tab 在文章标签中

PL-2344-444 40 (%) PL-2344-444 40(%)

P-0455-23 200 (SEK) P-0455-23 200(瑞典克朗)

In the category tab 在类别标签中

C12 50 (%) C12 50(%)

N12 35 (%) N12 35(%)

Today I have three different queries to see if I get a hit in the discount list: 今天,我有三个不同的查询,以查看是否在折扣列表中获得成功:

First I check to see if I get a hit in priority 1: (FIXED returns f and PERCENTAGE r) 首先,我检查是否在优先级1上获得成功:(固定返回f和PERCENTAGE r)

SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG 
WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013'

If the above returns 0, I do the second query, priority 2: 如果以上返回0,则执行第二个查询,优先级2:

SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG 
WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013'

And the last one priority 3: 最后一个优先事项3:

SELECT DISCOUNT, FIXED, PERCENTAGE FROM PUPRIREG 
WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013'

If none of the extra discount lists returns 0 I get the discount from the pricelist 如果没有任何额外折扣列表返回0,我将从价格列表中获得折扣

SELECT DISCOUNT FROM PUPRIREG WHERE PRICELIST = '01' AND PRICEGROUP = 'F' 从PUPRIREG的PRICELIST ='01'和PRICEGROUP ='F'的地方选择折扣

I call the function like follows $discount = discount($articlecode, $category, $pricegroup); 我这样调用函数:$ discount = discount($ articlecode,$ category,$ pricegroup);

function discount($articlecode, $category, $pricegroup){

$articlecode = sanitizingData($articlecode); 

$category = sanitizingData($category);

$pricegroup = sanitizingData($pricegroup);

// do priority 1

// prio 2

// prio 3

// pricelist

return $discount;

}

I would be so happy if someone could show me how to do this. 如果有人可以告诉我该怎么做,我会很高兴。 I am using mysqli and php. 我正在使用mysqli和php。

Many thanks 非常感谢

Best regards linda 最好的问候琳达

You can do the queries with a union: 您可以使用联合进行查询:

(SELECT DISCOUNT, FIXED, PERCENTAGE, 1 priority FROM PUPRIREG 
    WHERE ARTCODE = 'JO-23455' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 2 priority FROM PUPRIREG 
    WHERE CATEGORY = 'C15' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, FIXED, PERCENTAGE, 3 priority FROM PUPRIREG 
    WHERE PRICEGROUP = 'F' AND DISCOUNTLIST = '0013')
union
(SELECT DISCOUNT, 0 fixed, 0 percentage, 4 priority FROM PUPRIREG
    WHERE PRICELIST = '01' AND PRICEGROUP = 'F')
order by priority;

The additional artificial priority column and order by ensures, that you get the discounts properly sorted. 附加的人工priority列和顺序可确保您获得正确排序的折扣。

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

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