从2个不同的表中乘以值

时间:2011-04-03 21:25:42

标签: php sql

我对PHP和SQL非常陌生,所以我正在敲打着关于如何实现我正在寻找的结果的问题。我正在开发产品库存网站。该网站允许您输入收到的订单/数量,并更新特定产品的总库存。

有一个“交易”表;例如收到更多产品和“产品”表。

我试图将产品的现有数量乘以该产品的单价,以便显示该产品系列的总投资额。

这是我正在使用的代码 - Transactions表是site_trans,Products表是site_products:

<form>

<title>Products List</title>
<border>true</border>
<width>580</width>

<fields>
    <_count header="No" suffix="." width="25" align="center" />
    <product_name align="left" header="Name" />
    <product_number align="center" header="Number" />
    <product_count align="center" type="sql" header="Count">
        <sql>
            <vars>
                <table import="$this->tables[transactions]" type="eval" />
                <product import="product_id" type="field" />
            </vars>

            <query>SELECT sum(trans_inventory) FROM {TABLE} WHERE trans_product='{PRODUCT}'</query>
            <field>sum(trans_inventory)</field>
        </sql>

    </product_count>
    <product_cost align="center" header="Cost" />
    <product_uom align="center" header="Unit" />
    <product_totcost align="center" type="sql" header="Total">
        <sql>   
            <vars>
                <table import="$this->tables[transactions]" type="eval" />
                <prodcost import="product_cost" type="field" />
            </vars>
            <query>SELECT sum(trans_inventory), sum('{PRODCOST}') FROM site_trans, site_products WHERE trans_product=product_id</query>
            <field>sum(trans_inventory*product_cost)</field>
        </sql>
    </product_totcost>

</fields>

<header>
    <titles>true</titles>

    <buttons>
        <export onclick="" button="export">
            <location>index.php?sub=export&action=products</location>
        </export>

        <add button="addnew">               
            <location>index.php?sub=products&action=add&returnurl={CURRENT_PAGE}</location>
        </add>
    </buttons>
</header>

<buttons>

    <delete onclick="" button="delete" >
        <location>javascript:if(confirm('Are you sure you want to delete this record ?')) window.location='index.php?sub=products&action=delete&product_id={PRODUCT_ID}&returnURL={CURRENT_PAGE}'</location>
    </delete>

    <details onclick="" button="details" >
        <location>index.php?sub=products&action=details&section=1&product_id={PRODUCT_ID}&returnurl={CURRENT_PAGE}</location>
    </details>

</buttons>

<items>25</items>

给我问题的代码的特定部分是:

        <product_totcost align="center" type="sql" header="Total">
        <sql>   
            <vars>
                <table import="$this->tables[transactions]" type="eval" />
                <prodcost import="product_cost" type="field" />
            </vars>
            <query>SELECT sum(trans_inventory), sum('{PRODCOST}') FROM site_trans, site_products WHERE trans_product=product_id</query>
            <field>sum(trans_inventory*product_cost)</field>
        </sql>
    </product_totcost>

请帮助! :)

添加到此;这是事务的代码(add.xml),用于更新site_trans数据库中每个产品的库存数量。也许这段代码会揭示我们所遗漏的东西(我不知道)

<form>
<title>{PRIVATE.FORM_ACTION_TITLE} Transaction</title>
<action>index.php?sub=transactions&action=store</action>
<name>edit</name>
<width>580</width>

<formtag>true</formtag>
<border>true</border>

<buttons>       
    <set>
        <footer>true</footer>
        <header>true</header>
    </set>

    <return onclick="" button="return">
        <location>{PRIVATE.FORM_PREVIOUS_PAGE}</location>
    </return>

    <save onclick="" button="saveall">
        <location>javascript:document.forms[0].submit();</location>
    </save>

</buttons>

<redirect>index.php?sub=accounts</redirect>

<fields>
    <trans_id type="hidden" hidden="true"/>
    <trans_date type="hidden" default="time()" action="eval"/>
    <trans_user type="hidden" default="$_SESSION[minibase][raw][user_id]" action="eval"/>
    <trans_post_ip type="hidden" default="$_SERVER[REMOTE_ADDR]" action="eval"/>
    <trans_relation type="hidden" default="$_GET[trans_relation]" action="eval"/>
    <trans_product type="hidden" default="$_GET[trans_relation]" action="eval"/>

    <trans_inventory type="textbox" size="5" required="true" validate="A:2:200">            
        <title>Inventory</title>            
        <description>Box to either add (+40) or remove inventory (-40).</description>
    </trans_inventory>

    <trans_date2 type="date" now="true">
        <title>Date</title>

        <fields>
            <month format="int" default="now" />
            <day format="int" default="now" />
            <year from="2011" to="2110" default="now"/>
        </fields>
    </trans_date2>


    <trans_description type="textarea" size="60:4">         
        <title>Description</title>          
    </trans_description>

</fields>

2 个答案:

答案 0 :(得分:0)

我不确定你使用的是什么框架,但也许是这样的......

<query>SELECT sum(trans_inventory*product_cost), sum('{PRODCOST}') FROM site_trans, site_products WHERE trans_product=product_id</query>
<field>sum(trans_inventory*product_cost)</field>

这只是猜测。

编辑:

<query>SELECT sum(st.trans_inventory*sp.product_cost) as total, sum('{PRODCOST}') FROM site_trans as st, site_products as sp WHERE st.trans_product=sp.product_id</query>
<field>total</field>

答案 1 :(得分:0)

虽然您没有提到原始问题中的错误以及有问题的代码:

        <product_totcost align="center" type="sql" header="Total">
        <sql>   
            <vars>
                <table import="$this->tables[transactions]" type="eval" />
                <prodcost import="product_cost" type="field" />
            </vars>
            <query>SELECT sum(trans_inventory), sum('{PRODCOST}') FROM site_trans, site_products WHERE trans_product=product_id</query>
            <field>sum(trans_inventory*product_cost)</field>
        </sql>
    </product_totcost>

从您回答之前的回复,您似乎需要对您的字段名称进行限定,因此可能的解决方案可能是:

<query>SELECT sum(st.trans_inventory*sp.product_cost), sum('{PRODCOST}') FROM site_trans st, site_products sp WHERE st.trans_product=sp.product_id</query>
<field>sum(st.trans_inventory*sp.product_cost)</field>