English 中文(简体)
如何对待这一具体问题
原标题:how to approach this specific query
  • 时间:2010-01-19 17:26:14
  •  标签:
  • sql
  • mysql

我有一个相当复杂的逻辑质问一米试图执行。 基本上有一系列相关表格。 我很难找到解决这一问题的好办法。

表:

Transaction -
T_id

Discount -
D_id
item_id
Type (percentage or basic value)
value

Transaction_Discount -
T_id
D_id

Item -
item_id
price

EDIT: additional table
Purchase
T_id
item_id

我试图在适用折扣后收回调整的价格。 我愿避免在数据库中增加一个调整价格的栏目......因为从已经输入的数据中计算每笔交易调整价格(因此储存这些数据是多余的)。

这是基本逻辑:

if(this transaction had a discount applied)  
  //apply the discount and return the adjusted price  
else(no discount applied)  
  //pull price

在使用购买力平价逻辑的几次单独查询中可以这样做。

//1st step - create an array of bool s: true = discount_used/false = no_discount
//2nd step - if(discount_used) return price adjusted to discount
//3rd step - if(no_discount) return price
//4th step - combine the two different arrays

这显然非常庞大。 看来,哈萨克族民主党就是这样做的更好方式,这样做可能提高效率。

我的理解是,你能够提出我方言中含有逻辑的疑问,这是否有助于这里?

最佳回答

请允许我指出,您在交易表上有一个项目,而且这个百分比由P代表,你可以尝试像P一样的东西。

SELECT  *,
        CASE 
            WHEN td.T_id IS NULL
                THEN it.price
            WHEN td.T_id IS NOT NULL AND d.Type =  P 
                THEN id.price - (id.price * d.value)
            ELSE id.price - d.value
        END PriceDiscounted

FROM    Transaction t LEFT JOIN
        Purchase p ON t.T_id = p.T_id LEFT JOIN
        Item it ON p.item_id = it.item_id LEFT JOIN
        Transaction_Discount td ON t.T_id = td.T_id LEFT JOIN
        Discount d ON td.D_id = d.D_id LEFT JOIN
        Item id ON d.item_id = id.item_id
问题回答

德雷克,我建议设立一个储存职能。 在建立职能之后,你可以简单地这样做:

select item_id, price_after_discount(item_id) from item where ....

采取这种做法:

select i1.item_id, 
    i1.Price, 
    case when dt.item_id is null then i1.Price else i1.Price - dt.TotalDiscount end as DiscountedPrice
from Item i1
left outer join (
    select i.item_id, sum(
        case when d.type =  percentage  then i.price * d.value / 100 else d.value end
    ) as TotalDiscount
    from Item i
    inner join Discount d on i.item_id = d.item_id
    group by i.item_id
) dt on i1.item_id = dt.item_id

我不认为你可以。 具体来说,如果没有折扣,你就无法参加项目_id,因为与项目_id有关的唯一表格是<代码>Item table和Discount

如果你想要有没有折扣的交易,那么你将需要改变如何建立表格。

您可以尝试:

Item table:
  item_id
  price

Transaction table:
  T_id
  item_id
  D_id

Discount table:
  D_id
  type
  value

除非你想对某一特定项目的每一个情况都适用特别折扣。 然后,你想做这样的事情:

Item table:
  item_id
  price
  D_id (can be null)

Transaction table:
  T_id
  item_id

Discount table:
  D_id
  type
  value

另一种做法是,作为单独查询和结合结果,履行两个条件,例如,按照(当然对你的表格进行调整)的思路。

SELECT unadjustedPrice as Price FROM Transaction WHERE noDiscount = true
UNION ALL
SELECT unadjustedPrice - discount as Price From Transaction WHERE noDiscount = false

当然,你的真正询问会更加复杂,因为你需要加入适当的表格,但这应当给你以一般性做法。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签