一个建表的问题
问?:
我现在有3个表
product
id
name
provider_order
id
product_id fk
provider_price
customer
id
product_id fk
customer_price
我想用这3个表生成这样另外一个表
id product_name provider_price customer_price price_gap
其中price_gap = provider_price - customer_price
请问SQL怎么写
product
id
name
provider_order
id
product_id fk
provider_price
customer
id
product_id fk
customer_price
我想用这3个表生成这样另外一个表
id product_name provider_price customer_price price_gap
其中price_gap = provider_price - customer_price
请问SQL怎么写
答!: 1:
你product表中那个name字段是不是就是下面想新建表的的product_name字段啊?
在ORACLE数据库中~如果是的话,你运行下面这个:
create table new_table_name
AS select id,product_name,provider_price,customer_price,provider_price-customer_price " price_gap"
from product,provider_order,customer
where provider_order.id=customer.id
where语句是可以变化的,看你的业务逻辑了,注意你的主外键的约束,你有可能存在了某种商品(假使是供货商提供了一次商品编号为001的商品,价格为1元),但若这中商品没有被消费着买过,那么消费表中是没有这个商品的,所以我那样写where语句,还有一种情况是,只要存在了商品编号,那么就定义下供货商价格和消费价格,这样的话就不用写where语句了.若这是个小练习的话,做一做还可以,但要是个小型项目的话,这个数据库中的几个表是有些问题的!我没看到你建表的关系,所以以上都是猜测,所以~你在运行where条件语句时可以根据自己定义的业务逻辑方式!
在ORACLE数据库中~如果是的话,你运行下面这个:
create table new_table_name
AS select id,product_name,provider_price,customer_price,provider_price-customer_price " price_gap"
from product,provider_order,customer
where provider_order.id=customer.id
where语句是可以变化的,看你的业务逻辑了,注意你的主外键的约束,你有可能存在了某种商品(假使是供货商提供了一次商品编号为001的商品,价格为1元),但若这中商品没有被消费着买过,那么消费表中是没有这个商品的,所以我那样写where语句,还有一种情况是,只要存在了商品编号,那么就定义下供货商价格和消费价格,这样的话就不用写where语句了.若这是个小练习的话,做一做还可以,但要是个小型项目的话,这个数据库中的几个表是有些问题的!我没看到你建表的关系,所以以上都是猜测,所以~你在运行where条件语句时可以根据自己定义的业务逻辑方式!
答!: 2:
我犯了个低级错误(大体没错,一个小细节)~不好意思啊~
应该这样修改
create table new_table_name
AS select product.id,product.name "product_name",provider.provider_price,customer.customer_price,provider_price.provider_price-customer.customer_price "price_gap"
from product,provider_order,customer
where provider_order.id=customer.id
是多表的,要加表名.列名,不好意思~我搞数据库也一般的,有机会多交流!
应该这样修改
create table new_table_name
AS select product.id,product.name "product_name",provider.provider_price,customer.customer_price,provider_price.provider_price-customer.customer_price "price_gap"
from product,provider_order,customer
where provider_order.id=customer.id
是多表的,要加表名.列名,不好意思~我搞数据库也一般的,有机会多交流!
相关JAVA教程:
调试roller2.3版的blog问题
eclipse+weblogic 开发j2ee时怎么生成ejb-jar.xml等描述符文件?
Hibernate+Struts 使用PropertyUtils拷贝Form表单对象至VO类 保存至数据库时Date类型无法转换?
"0x004385ae"指令引用的"0X0000014"内存.该内存不能为"read".
在JBoss4.0.4中怎样设置JMS-MDB实例池的上限?
如何改变JFileChooser大小
用什么技术开发动画
求助:不要dtd文件,如何留住xml中的实体引用?
数据库问题让人难以理解
求助----JTable中每次从数据库中取到不同的数据 怎样清空上次记录?
怎么读取Spring 的配置文件applicationContext.xml
很奇怪的问题,java的程序,在台式机上按TAB键是按顺序走,但是在笔记本上却不是,什么原因,谢谢!