值未插入正确的MySQL记录中

时间:2018-08-31 11:37:35

标签: mysql sql

将值插入表中时,不是将值分配给单个记录,而是将值分配给所有记录:

CREATE TABLE IF NOT EXISTS user
(
    userID VARCHAR(50) NOT NULL,
    userName VARCHAR(40) NOT NULL,        
    PRIMARY KEY (userID)
);
    CREATE TABLE IF NOT EXISTS rewards
(
    rewardID VARCHAR(5) NOT NULL,
    rewardDescription VARCHAR(20) NOT NULL,
    PRIMARY KEY (rewardID)
);

CREATE TABLE IF NOT EXISTS promos
(   
    issueDate DATETIME DEFAULT CURRENT_TIMESTAMP,
    expiryDateOfReward DATETIME GENERATED ALWAYS AS(issueDate + INTERVAL 6 MONTH),  
    quantity BIT DEFAULT 0,
    userID VARCHAR(50) REFERENCES user(userID),
    rewardID VARCHAR(5) REFERENCES rewards(rewardsID),
    PRIMARY KEY(userID, rewardID)
);

    INSERT INTO user
    VALUES('DV2015', 'Bob'),
          ('DV2016', 'Mary'),
          ('DV2017', 'Megan');

INSERT INTO rewards
VALUES('rw10', '10% Off'),
      ('rw20', '20% Off'),
      ('rw30', '30% Off');

INSERT INTO promos (issueDate, quantity, userID, rewardID)
            VALUES(DEFAULT, DEFAULT, 'DV2016', 'rw10'),
                  (DEFAULT, 1, 'DV2015', 'rw20'),
                  (DEFAULT, DEFAULT, 'DV2017', 'rw30'); 

然后我选择它:

SELECT userName, 
       rewards.rewardID, 
       rewards.rewardDescription, 
       promos.quantity 
  FROM user
  JOIN promos
    ON user.userID = promos.userID
  JOIN rewards
    ON promos.rewardID = promos.rewardID
 WHERE user.userID = 'DV2015';

我不知道错误是在表的创建,插入记录还是在select语句中。 它是这样输出的:

enter image description here

我只希望用户ID为“ DV2015”的Bob在“ rw20”上的“数量”为1。 rw10和rw30的数量仍然必须为0。但是,在插入数据时,它会给他所有的奖励

4 个答案:

答案 0 :(得分:2)

您在这里有意外的交叉加入:

JOIN rewards
    ON promos.rewardID = promos.rewardID

当然,所有促销记录都适用。您可能是说:

JOIN rewards
    ON rewards.rewardID = promos.rewardID

如果您希望“ rw10和rw30的数量仍然必须为0”,则应切换到LEFT JOIN而不是(INNER) JOIN

答案 1 :(得分:1)

从促销表中加入

SELECT u.userName, 
   r.rewardID, 
   r.rewardDescription, 
   p.quantity 
FROM promos p
JOIN user u ON u.userID = p.userID
JOIN rewards r ON r.rewardID = p.rewardID
WHERE u.userID = 'DV2015';

答案 2 :(得分:0)

您在向表中插入数据时犯了错误

insert into table_name values()//if you provide values all the column

否则

insert into table_name (col1,col2...coln) values('use1','use2'....n)

因此,在您的情况下,用户表值将如下所示插入

INSERT INTO user values(1,'user_name1'),(2,'user_name2');

其他两个表(奖励,促销)的数据插入过程有效

答案 3 :(得分:0)

从您的预期结果中,您可以尝试一下。

CROSS JOINuserrewards表以笛卡尔产品结果。

然后根据promos结果集OUTER JOINCROSS JOIN

CREATE TABLE user
(
    userID VARCHAR(50) NOT NULL,
    userName VARCHAR(40) NOT NULL  
);
CREATE TABLE  rewards
(
    rewardID VARCHAR(5) NOT NULL,
    rewardDescription VARCHAR(20) NOT NULL
);

CREATE TABLE promos
(   
    issueDate DATETIME DEFAULT CURRENT_TIMESTAMP,
    expiryDateOfReward DATETIME ,  
    quantity int DEFAULT 0,
    userID VARCHAR(50),
    rewardID VARCHAR(5)
);

    INSERT INTO user
    VALUES('DV2015', 'Bob'),
          ('DV2016', 'Mary'),
          ('DV2017', 'Megan');

INSERT INTO rewards
VALUES('rw10', '10% Off'),
      ('rw20', '20% Off'),
      ('rw30', '30% Off');

INSERT INTO promos (issueDate, quantity, userID, rewardID)
            VALUES(0, 0, 'DV2016', 'rw10'),
                  (0, 1, 'DV2015', 'rw20'),
                  (0, 0, 'DV2017', 'rw30'); 

查询1

SELECT userName, 
       r.rewardID, 
       r.rewardDescription, 
       coalesce(p.quantity,0) quantity
  FROM user u CROSS JOIN rewards r
  LEFT JOIN promos p
    ON u.userID = p.userID and r.rewardID = p.rewardID
 WHERE u.userID = 'DV2015'

Results

| userName | rewardID | rewardDescription | quantity |
|----------|----------|-------------------|----------|
|      Bob |     rw20 |           20% Off |        1 |
|      Bob |     rw10 |           10% Off |        0 |
|      Bob |     rw30 |           30% Off |        0 |