【问题标题】:How to set the value of an attribute according to the values of other attributes in SQL?SQL中如何根据其他属性的值来设置一个属性的值?
【发布时间】:2021-04-01 10:57:20
【问题描述】:

我正在尝试根据其他属性的值来设置属性的值。

我有以下SQL语句,

SELECT C.course_name, C.language, C.course_price, C.average_rating, C.category, P.name, P.surname, D.percentage, D.start_date, D.end_date, D.is_allowed
FROM course C LEFT OUTER JOIN discount D ON C.course_id=D.discounted_course_id, course_creator CC, person P
WHERE C.course_creator_id=CC.course_creator_id AND CC.course_creator_id=P.person_id

我有以下结果,

预期结果同上,但是第一行的 course_price 是 500。

我想根据以下设置 course_price:

如果当前日期小于 end_date 并且 is_allowed = 1,那么我想将折扣百分比应用于 course_price。例如,由于第一个元组的 course_price 为 1000,因此百分比为 50,(假设 end_date 大于今天的日期),并且 is_allowed=1。那么 course_price 应该等于 500。

课程关系:

课程创建者关系:

折扣关系:

人际关系:

如果折扣关系具有课程的 course_id 并且 is_allowed 为 1,并且折扣的 end_date 大于今天的日期,我想获取所有课程及其信息并将这些课程的 course_price 设置为折扣价格

【问题讨论】:

  • 请提供样本数据以及期望的结果。同时标记您正在使用的数据库。
  • 不要将显式 JOIN 与隐式、逗号分隔的 JOIN 混合。太混乱了。到处使用明确的JOIN 语法。
  • 你能再回顾一下吗?我确实提供了样本数据。
  • 这里的大多数人希望样本表数据和预期结果为格式化文本,而不是图像。此外,尽可能简化,即minimal reproducible example.

标签: sql database mariadb innodb


【解决方案1】:

如果当前日期小于 end_date 并且 is_allowed = 1,那么我想将折扣百分比应用于 course_price。

你似乎想要一个CASE 表达式:

SELECT C.course_name, C.language, C.course_price, C.average_rating, C.category,
     P.name, P.surname,
     D.percentage, D.start_date, D.end_date, D.is_allowed,
     (CASE WHEN current_date < D.end_date and D.is_allowed
           THEN C.course_price * D.percentage
           ELSE c.course_price
      END) as imputed_price
FROM course C LEFT OUTER JOIN
    discount D 
    ON C.course_id = D.discounted_course_id LEFT JOIN
    course_creator CC
    ON C.course_creator_id = CC.course_creator_id LEFT JOIN
    person P
    ON CC.course_creator_id = P.person_id;

请注意,这也修复了JOIN 语法。 永远不要在FROM 子句中使用逗号!

【讨论】:

  • 如果我想在 imputed_price 中添加过滤器应该怎么做。我尝试添加 WHERE imputed_price
  • @MustafaYaşar 。 . .一个简单的方法是 CTE 或子查询。
【解决方案2】:

您可以为此使用 IF 或 CASE(SQL92 兼容)函数。许多 RDBMS 都有它。

这是一个取自here的MySql示例:

SELECT book_name,
IF(pub_lang='English',"English Book","Other Language") 
AS Language 
FROM book_mast;

数据:

+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
| book_id | book_name                           | isbn_no     | cate_id | aut_id | pub_id | dt_of_pub  | pub_lang | no_page | book_price |
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
| BK001   | Introduction to Electrodynamics     | 0000979001  | CA001   | AUT001 | P003   | 2001-05-08 | English  |     201 |      85.00 |
| BK002   | Understanding of Steel Construction | 0000979002  | CA002   | AUT002 | P001   | 2003-07-15 | English  |     300 |     105.50 |
| BK003   | Guide to Networking                 | 0000979003  | CA003   | AUT003 | P002   | 2002-09-10 | Hindi    |     510 |     200.00 |
| BK004   | Transfer  of Heat and Mass          | 0000979004  | CA002   | AUT004 | P004   | 2004-02-16 | English  |     600 |     250.00 |
| BK005   | Conceptual Physics                  | 0000979005  | CA001   | AUT005 | P006   | 2003-07-16 | NULL     |     345 |     145.00 |
| BK006   | Fundamentals of Heat                | 0000979006  | CA001   | AUT006 | P005   | 2003-08-10 | German   |     247 |     112.00 |
| BK007   | Advanced 3d Graphics                | 0000979007  | CA003   | AUT007 | P002   | 2004-02-16 | Hindi    |     165 |      56.00 |
| BK008   | Human Anatomy                       | 0000979008  | CA005   | AUT008 | P006   | 2001-05-17 | German   |      88 |      50.50 |
| BK009   | Mental Health Nursing               | 0000979009  | CA005   | AUT009 | P007   | 2004-02-10 | English  |     350 |     145.00 |
| BK010   | Fundamentals of Thermodynamics      | 0000979010  | CA002   | AUT010 | P007   | 2002-10-14 | English  |     400 |     225.00 |
| BK011   | The Experimental Analysis of Cat    | 0000979011  | CA004   | AUT011 | P005   | 2007-06-09 | French   |     225 |      95.00 |
| BK012   | The Nature  of World                | 0000979012  | CA004   | AUT005 | P008   | 2005-12-20 | English  |     350 |      88.00 |
| BK013   | Environment a Sustainable Future    | 0000979013  | CA004   | AUT012 | P001   | 2003-10-27 | German   |     165 |     100.00 |
| BK014   | Concepts in Health                  | 0000979014  | CA005   | AUT013 | P004   | 2001-08-25 | NULL     |     320 |     180.00 |
| BK015   | Anatomy & Physiology                | 0000979015  | CA005   | AUT014 | P008   | 2000-10-10 | Hindi    |     225 |     135.00 |
| BK016   | Networks and Telecommunications     | 00009790_16 | CA003   | AUT015 | P003   | 2002-01-01 | French   |      95 |      45.00 |
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+

结果:

+-------------------------------------+----------------+
| book_name                           | Language       |
+-------------------------------------+----------------+
| Introduction to Electrodynamics     | Engllish Book  | 
| Understanding of Steel Construction | Engllish Book  | 
| Guide to Networking                 | Other Lnaguage | 
| Transfer  of Heat and Mass          | Engllish Book  | 
| Conceptual Physics                  | Other Lnaguage | 
| Fundamentals of Heat                | Other Lnaguage | 
| Advanced 3d Graphics                | Other Lnaguage | 
| Human Anatomy                       | Other Lnaguage | 
| Mental Health Nursing               | Engllish Book  | 
| Fundamentals of Thermodynamics      | Engllish Book  | 
| The Experimental Analysis of Cat    | Other Lnaguage | 
| The Nature  of World                | Engllish Book  | 
| Environment a Sustainable Future    | Other Lnaguage | 
| Concepts in Health                  | Other Lnaguage | 
| Anatomy & Physiology                | Other Lnaguage | 
| Networks and Telecommunications     | Other Lnaguage | 
+-------------------------------------+----------------+

【讨论】:

  • "很多 RDBMS 都有它"? - 我认为只有 MySQL 支持非标准 IF() - 但是所有 RDBMS 都支持标准 CASE 表达式。
  • 如果没有“IF”,可以使用“CASE”。
  • @AbylaySabirgaliyev 先生,我知道我应该 IF 或 CASE,但是如何?
  • @MustafaYaşar 通过为函数提供表达式和结果。读一读。
【解决方案3】:

由于我在您的问题中没有看到确切的 RDBMS,所以我会给您一个 应该 在 mssql 服务器中工作的查询(请注意,我无法使用准确的数据验证查询,所以我可能有拼写错误)。

在 mssql 中,您可以很容易地将选择转换为更新。可能也在其他数据库中。

UPDATE C
SET course_price = course_price * ((100-percentage) / 100)
FROM course C 
LEFT OUTER JOIN discount D ON C.course_id=D.discounted_course_id, 
course_creator CC, person P
WHERE C.course_creator_id=CC.course_creator_id 
    AND CC.course_creator_id=P.person_id
    AND end_date < GETDATE()
    AND is_allowed = 1
    AND C.course_id = course.course_id -- course is the table in the update statement

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多