dongpai6567 2013-07-25 07:19
浏览 26

MySQL从两个现有的表计算中创建一个表

I'm making a php web page with Yii and MySQL. My problem is in database part.

I have two tables, "stock" and "given". They are like:

stock = id (primary key), name, amount (this is integer), details

and

given = id (from stock), name (from stock), amount (given amount, thus not same with stock), ...

I want to create a table "leftStock" which is the same format with "stock" but has different amount. It will be the number left in stock after given some. Since in different times, different amounts of the same item may be given, the given table's id and name is not unique. Thus i'll use "sum(given.amount)" but its not definite for me.

I thought i can use "create table...select" style coding but i could not construct its structure. Can anybody help me?

(Also can i use "view" in a Yii web page? Because i won't make any direct change on "leftStock" table, its being view may be feasible either.)

  • 写回答

1条回答 默认 最新

  • duanjizi9443 2013-07-25 07:29
    关注

    It should be done by creating a query too.

    Try this, I dont test it yet:

    SELECT 
        (SELECT SUM(a.amount - b.amount) 
        FROM stock AS a, given AS b 
        WHERE a.id = b.id AND a.id = id) AS `itemleft`, *
    FROM stock
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么