Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何加入两个表以获得日销售_Mysql_Sql - Fatal编程技术网

Mysql 如何加入两个表以获得日销售

Mysql 如何加入两个表以获得日销售,mysql,sql,Mysql,Sql,我想有条件地连接两个表 表1 CREATE TABLE IF NOT EXISTS `payment` ( `paymentcode` int(6) NOT NULL, `date` int(3) unsigned NOT NULL, `amount` varchar(200) NOT NULL, `customer` varchar(200) NOT NULL, `store` varchar(200) NOT NULL ) ('2', '20190120',

我想有条件地连接两个表

表1

CREATE TABLE IF NOT EXISTS `payment` (
  `paymentcode` int(6)  NOT NULL,
  `date` int(3) unsigned NOT NULL,
  `amount` varchar(200) NOT NULL,
  `customer` varchar(200) NOT NULL,
  `store` varchar(200) NOT NULL
 )


  ('2', '20190120', '10050','C1','A'),
  ('2', '20190120', '10050','c2','A'),
  ('6', '20190120', '9050','c3','A'),
  ('4', '20190120', '9045','c4','B'),
  ('6', '20190121', '10050','c5','B'),
  ('2', '20190121', '20050','c6','A');
表2

CREATE TABLE IF NOT EXISTS `customer` (
  `code` int(6)  NOT NULL,
  `name` int(3) NOT NULL,
  )


  ( 'C1','Customer1'),
  ( 'c2','Customer2'),
  ( 'c3','Customer3'),
  ( 'c4','Customer4'),
  ( 'c5','Customer5'),
  ( 'c6','Customer6');
从上面的查询中,我可以获取以下值:

date     paymentcode  Amount           documet_total
20190120     2         20100               29150
20190120     4          9045                9045
20190120     6         18095               29150
20190121     4         20050               20050
20190121     2         10050               10050
这就是我试图提出的问题。现在,如果Paymentcode='2'还需要存储值,我想加入customer表以获取客户代码

我的预期结果如下:

date     paymentcode  Amount customer_type  document_total
20190120     2         10050     C1           29150
20190120     2         10050     C2           29150
20190120     4          9045      B            9045
20190120     6         18095      A           29150
20190121     4         20050      B           20050
20190121     2         10050     C6           10050
如果我需要根据日期、门店、客户类型和付款代码计算金额,则客户类型应为客户编码it Paymentcode='2'否则门店代码,则凭证总额基于日期、门店


请告诉我如何加入这些表并获得输出

如果您是sql server的新手,我强烈建议您使用向导而不是运行查询来加入两个表。我假设您的付款表中的客户实际上是客户代码?i、 e客户的FK?另外,在查看数据结构时,您确实需要了解规范化、键和实体,以及这些记录应包含哪些类型的数据。您确定要使用SQL Server吗?T-SQL不使用倒勾(
`
)来分隔标识;它使用括号(
[]
int
在SQL Server中不支持长度/精度值,
unsigned
也不是该数据类型的属性。SQL Server仅支持有符号的数字数据类型(除了
tinyint
,它是无符号的,只允许值
0
-
255
)。同意,这与SQL Server不同。是MySQL吗?如果不存在创建表不是SQL Server语法
date     paymentcode  Amount customer_type  document_total
20190120     2         10050     C1           29150
20190120     2         10050     C2           29150
20190120     4          9045      B            9045
20190120     6         18095      A           29150
20190121     4         20050      B           20050
20190121     2         10050     C6           10050