[前][次][番号順一覧][スレッド一覧]

mysql:10435

From: "konet218@xxxxxxxxxx" <"konet218@xxxxxxxxxx" <konet218@xxxxxxxxxx>>
Date: Tue, 16 Nov 2004 07:15:04 +0900
Subject: [mysql 10435] IF()を使ったSELECT文について

近藤です。

ちょっと途方にくれてしまいました。
おそらく意図したSQL文に間違いはないと思っているのですが、
IF()が思う様に言う事を聞いてくれません。

[環境]
MySQL4.0.20

[テーブル構造]
CREATE TABLE `acc_tbl` (
  `acc_media_code` varchar(5) binary NOT NULL default '',
  `acc_store_id` bigint(20) unsigned NOT NULL default '0',
  `acc_career` enum('i','v','e') NOT NULL default 'i',
  `acc_date` date NOT NULL default '0000-00-00',
  `acc_time` tinyint(2) unsigned NOT NULL default '0',
  `acc_num` bigint(20) unsigned NOT NULL default '0'
) TYPE=MyISAM;

[データ]
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'e', '2004-11-01', 6, 2);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'i', '2004-11-01', 8, 1);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'i', '2004-11-01', 9, 1);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'v', '2004-11-06', 21, 1);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'i', '2004-11-06', 22, 15);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'i', '2004-11-06', 23, 2);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'v', '2004-11-07', 0, 1);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'e', '2004-11-07', 10, 1);
INSERT INTO `acc_tbl` VALUES ('zzz', 0, 'i', '2004-11-07', 11, 2);

[SQL]
SELECT
 IF(`acc_date`='2004-11-07',SUM(`acc_num`),0) AS TODAY,
 IF(`acc_date`='2004-11-07' && `acc_career`='i',SUM(`acc_num`),0) AS TODAY_I,
 IF(`acc_date`='2004-11-07' && `acc_career`='v',SUM(`acc_num`),0) AS TODAY_V,
 IF(`acc_date`='2004-11-07' && `acc_career`='e',SUM(`acc_num`),0) AS TODAY_E,
 IF(`acc_date`='2004-11-06',SUM(`acc_num`),0) AS YESTERDAY,
 IF(`acc_date`<>'2004-11-07' && `acc_date`<>'2004-11-06',SUM(`acc_num`),0) AS TOTAL
 FROM `acc_tbl`;

[実行結果]
+---------------------------------------------------------+
| TODAY | TODAY_I | TODAY_V | TODAY_E | YESTERDAY | TOTAL |
+-------+---------+---------+---------+-----------+-------+
|     0 |       0 |       0 |       0 |         0 |    26 |
+-------+---------+---------+---------+-----------+-------+

[期待する結果]
+---------------------------------------------------------+
| TODAY | TODAY_I | TODAY_V | TODAY_E | YESTERDAY | TOTAL |
+-------+---------+---------+---------+-----------+-------+
|     4 |       2 |       1 |       1 |        18 |     4 |
+-------+---------+---------+---------+-----------+-------+

何がおかしいのでしょうか?




[前][次][番号順一覧][スレッド一覧]

->   10435 2004-11-16 07:15 ["konet218@xxxxxxxxxx] IF()を使ったSELECT文について            
     10437 2004-11-16 09:15 ┗["A.K.I." <aki@xxxxxx]                                       
     10440 2004-11-16 09:32  ┗["konet218@xxxxxxxxxx]