我在 MySQL 中的视图不起作用
Posted
技术标签:
【中文标题】我在 MySQL 中的视图不起作用【英文标题】:My VIEW in MySQL its not working 【发布时间】:2010-12-15 02:47:57 【问题描述】:我在 mysql 5 中使用 CREATE VIEW,现在我遇到了麻烦,我如何确定何时使用此代码。
SELECT
*
FROM
view_shop_invoicer_list
WHERE
accept_date >= '2009-10-16 00:00:00' AND
accept_date <= '2009-10-31 23:59:59' AND
shopid = [SHOPID];
我的 VIEW 看起来像这样,在这里,它不会将我的 accept_date 带到这个视图中,我是否从我的 SELECTE 复制/粘贴它并将 int 插入到我的 VIEW 中它会起作用,但不是顺序方式:(
DROP VIEW IF EXISTS view_shop_invoicer_list;
CREATE VIEW view_shop_invoicer_list AS
SELECT
SUM( it.transamount ) AS beloeb,
SUM( it.cargo_fee ) AS cargo,
SUM(
(
it.transamount -
(
( it.transamount - it.cargo_fee ) / 100 *
( ms.pay_adm_fee + ms.pay_marks_fee + ms.pay_kick_fee ) -
( it.adm_fee + it.marketing_fee + it.kickback_fee )
)
)
) AS shop_payout,
SUM( fc_return_fee( it.transamount , it.cargo_fee , ms.pay_adm_fee , it.adm_fee ) ) AS shop_adm_fee,
SUM( fc_return_fee( it.transamount , it.cargo_fee , ms.pay_marks_fee , it.marketing_fee ) ) AS shop_marks_fee,
SUM( fc_return_fee( it.transamount , it.cargo_fee , ms.pay_kick_fee , it.kickback_fee ) ) AS shop_kick_fee,
it.shopid AS shopid,
it.accept_date AS accept_date
FROM
invoice_trans it INNER JOIN invoice i ON it.orderid = i.ordreid
INNER JOIN shops ms ON ms.id = it.shopid
WHERE
i.status != 0
我知道为什么它不起作用:/
表 1:invoice_trans
CREATE TABLE `invoice_trans` (
`id` int(11) NOT NULL auto_increment,
`orderid` varchar(32) NOT NULL default '0',
`transamount` int(11) NOT NULL default '0',
`transact` varchar(32) NOT NULL default '',
`transnr` int(11) NOT NULL default '0',
`shopid` int(11) NOT NULL default '0',
`status` int(11) NOT NULL default '0',
`accept_date` timestamp NOT NULL default '0000-00-00 00:00:00',
`md5_key_declie` varchar(32) NOT NULL,
`declie_date` datetime NOT NULL default '0000-00-00 00:00:00',
`pay_points` int(1) NOT NULL default '0',
`cargo_fee` int(11) NOT NULL default '4900',
`first_time_discount` int(2) NOT NULL default '0',
`adm_fee` int(11) NOT NULL default '0',
`marketing_fee` int(11) NOT NULL default '0',
`kickback_fee` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
表 2:发票
CREATE TABLE `invoice` (
`id` int(11) NOT NULL auto_increment,
`userid` int(11) NOT NULL default '0',
`per_fullname` varchar(64) NOT NULL,
`per_street` varchar(64) NOT NULL,
`per_zipcode` int(4) NOT NULL,
`per_city` varchar(64) NOT NULL,
`per_email` varchar(256) NOT NULL default '',
`fak_fullname` varchar(64) NOT NULL default '',
`fak_street` varchar(64) NOT NULL,
`fak_zipcode` int(4) NOT NULL,
`fak_city` varchar(64) NOT NULL,
`fak_email` varchar(256) NOT NULL default '',
`push_date` datetime NOT NULL default '0000-00-00 00:00:00',
`status` int(1) NOT NULL default '0',
`splits` int(11) NOT NULL default '0',
`dibs_ordreid` varchar(32) NOT NULL default '0',
`reftag` varchar(64) NOT NULL,
`vonchers_prices` int(11) NOT NULL default '0',
`ordre_complate` int(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
【问题讨论】:
我没有任何要测试的基表,但乍一看似乎它应该可以工作。也许您可以发布一个小而完整的 sql 脚本来重新创建所需的表并插入一些测试数据?我很乐意看看。 问题在于,当我使用它时,它取出 3 个 ordre,此 mdr 取出 2 个。最后一个( 16-31 - Okt )和 1 并且它仅关闭从 16 okt 到 31 okt 的 1 订单。 sum() 和 shop_id 参数不需要按列分组吗? 【参考方案1】:invoice
中没有可加入的 ordreid
(或 orderid
)列。顺便说一下脚本还是很不完整,没有函数fc_return_fee
或者测试数据,所以可测试性非常有限。
mysql 给你的错误信息是什么?
【讨论】:
那里没有错误,查看它的工作电流。但我需要得到正确的数据,现在当我使用 accept_date >= '' 和 accept_date 那么您可能需要在视图末尾添加一些代码:GROUP BY shopid, accept_date accept_date 可以在 2009 年 10 月 16 日和 2009 年 10 月 31 日之间,我不知道我怎么能对此感到沮丧,但是如果我可以将日期组合在一起,它会很好。 【参考方案2】:我发现了问题……我用这种方法解决了问题。
DROP VIEW IF EXISTS view_shop_invoicer_list;
CREATE VIEW view_shop_invoicer_list AS
SELECT
it.transamount AS beloeb,
SUM( it.cargo_fee ) AS cargo,
(
it.transamount -
(
( it.transamount - it.cargo_fee ) / 100 *
( ms.pay_adm_fee + ms.pay_marks_fee + ms.pay_kick_fee ) -
( it.adm_fee + it.marketing_fee + it.kickback_fee )
)
) AS shop_payout,
fc_return_fee( it.transamount , it.cargo_fee , ms.pay_adm_fee , it.adm_fee ) AS shop_adm_fee,
fc_return_fee( it.transamount , it.cargo_fee , ms.pay_marks_fee , it.marketing_fee ) AS shop_marks_fee,
fc_return_fee( it.transamount , it.cargo_fee , ms.pay_kick_fee , it.kickback_fee ) AS shop_kick_fee,
it.shopid AS shopid,
it.accept_date AS accept_date
FROM
invoice_trans it INNER JOIN invoice i ON it.orderid = i.ordreid
WHERE
i.status != 0
GROUP BY
it.id;
我使用这个选择 ;) 坦克寻求帮助。
SELECT
SUM( beloeb ) AS beloeb,
SUM( cargo ) AS cargo,
SUM( shop_payout ) AS shop_payout,
SUM( shop_adm_fee ) AS shop_adm_fee,
SUM( shop_marks_fee ) AS shop_marks_fee,
SUM( shop_kick_fee ) AS shop_kick_fee,
accept_date,
shopid
FROM
view_shop_invoicer_list
WHERE
accept_date >= '". $this->from_date ."' AND
accept_date <= '". $this->to_date ."'
GROUP BY
shopid
【讨论】:
以上是关于我在 MySQL 中的视图不起作用的主要内容,如果未能解决你的问题,请参考以下文章