微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在PHP Envirnment中使用来自另一个查询中的一个MySQL查询的结果

我有一个问题,它可能是一个简单的问题解决方案,但我似乎无法搞清楚.我是PHPMysqL的新手,所以我到处都在阅读所有内容,但缺乏经验非常令人沮丧,因为经常需要很长时间才能实现一个错误.请查看下表并阅读下面的问题.

PHP / MysqL在Joomla环境中,我正在尝试修改插件,因此将来自不同表的值更新为一组其他表,这些表原本不是预期的,但所有表都驻留在同一个数据库中.

   Table 1           vm_orders
---------------------------------------------
order_id    user_id     
---------------------------------------------
20          1           
55          6           
65          2           
30          4           
50          67          




   Table 2   vm_order_item
---------------------------------------------
order_item_id   order_id  order_item_sku    
---------------------------------------------
20                  20          1
55                  55          35
65                  65          60
30                  30          22
50                  50          3



Table 3 xipt_ users
---------------------------------------------------
userid  Profiletype template
----------------------------------------------------
1       1       default
6       3       default
2       1       default
4       8       default
67      7       default


Table 4      community_fields_values
---------------------------------------------
id      user_id     field_id    value
---------------------------------------------
1           1           55      Female
2           6           35      Cat
3           2           2       2
4           4           18      Texas
5           67          12      bike

我需要的是首先根据下订单的用户获取订单号.
userid变量正在从脚本的其他位置传递.那部分工作正常.
因此用户67已下订单.这些是我想要实现的目标.

>查询1:我想从vm_orders表(表1)的“order_id”列中获取“orderid”值;我将调用结果“vmorderid”并在另一个查询中使用它.
>查询2:使用查询1中的“vmorderid”作为vm_order_item表(表2)的“order_id”列中的order_id值.
           我想从my_order_item表(表2)的“order_item_sku”列中获取order_item_sku值.
           我将调用结果“vmsku”并在另一个查询中使用它.
>查询3:使用查询2中的“vmsku”作为vm_users表(表3)的“Profiletype”列中的profiletype值.
           我想更新“profiletype”列的值,其中包含“vmsku”值.
>查询4:使用来自查询2的“vmsku”作为community_fields_values(表4)的“value”列中的值.
            我想更新my_fields_values(表4)“vmsku”值中“value”列的值.

好的,我希望你到目前为止和我在一起,我已经尝试了几个问题,但它没有用.
这是我到目前为止:

假设用户正从param字段传递.

$userid = $this->params->get('userid', 'defaultValue'); 




function _vm_custom_order($vmorderId) 
{

    $vmorderId = $database->loadResult();
    $database = JFactory::getDBO();

    // query the db to see if the user is already a member of group

     $vmorderId ="

                SELECT MAX
                    `order_id` 
                FROM 
                    #__vm_orders';
                WHERE 
                    `user_id` = '{$userid}'
                    ";

     $database->setQuery( $vmorderId );
    $data = $database->loadResult();

    return $data;
}



function _vm_sku($vmsku) 
{

    $vmsku = $database->loadResult();
    $database = JFactory::getDBO();

    // query the db to see if the user is already a member of group
    $vmsku = "
                SELECT 
                    `product_sku` 
                FROM 
                    #__vm_order_item';
                WHERE 
                    `order_id` = '{$vmorderId}'

                    ";

      $database->setQuery( $vmsku );
    $data = $database->loadResult();

    return $data;

}



function _add( $userid, $groupid, $vmsku)
{
    $success = false;
    $database = JFactory::getDBO();

    if (!$allow_multiplegroups = $this->params->get( 'allow_multiplegroups', '1' )) {
        // query the db to see if the user is already a member of ANY group
        $database->setQuery("
            SELECT 
                `profiletype` 
            FROM 
                #__xipt_users
            WHERE 
                `userid` = '{$userid}' 
        ");
        $member = $database->loadResult();

        // if so, do not execute
        if (intval($member) > 0) {
            return $success;
        }
    }

    $already = plgAmbrasubsAddToXipt::_already( $userid, $groupid );

    if (($already != $userid)) 
    {
    $database->setQuery("

                SELECT MAX
                    `order_id` 
                FROM 
                    #__vm_orders
                WHERE 
                    `user_id` = '{$userid}'
                        ");

                    $vmorderId = $database->loadResult();

        if ($database->query()) {
            $success = true; 
        }
    }       


    if (($already != $userid)) 
    {

$database->setQuery("

                SELECT 
                    `product_sku` 
                FROM 
                    #__vm_order_item
                WHERE 
                    `order_id` = '{$vmorderId}'
                        ");                     

                    $vmsku = $database->loadResult();

        if ($database->query()) {
            $success = true; 
        }
    }           


    // if they aren't already a member of the group, add them to the group
    if (($already != $userid)) 
    {
        $database->setQuery("
            UPDATE 
                #__xipt_users
            SET
                `profiletype` = '{$vmsku}'

            WHERE
                `userid` = '{$userid}'

            LIMIT 1

        ");

        if ($database->query()) {
            $success = true; 
        }
    } 

    return $success;
}

}

我也这样试过:

function _add( $userid, $groupid, $vmsku)
{
    $success = false;
    $database = JFactory::getDBO();

    if (!$allow_multiplegroups = $this->params->get( 'allow_multiplegroups', '1' )) {
        // query the db to see if the user is already a member of ANY group
        $database->setQuery("
            SELECT 
                `profiletype` 
            FROM 
                #__xipt_users
            WHERE 
                `userid` = '{$userid}' 
        ");
        $member = $database->loadResult();

        // if so, do not execute
        if (intval($member) > 0) {
            return $success;
        }
    }

    $already = plgAmbrasubsAddToXipt::_already( $userid, $groupid );

    if (($already != $userid)) 
    {
    $database->setQuery("

                SELECT MAX
                    `order_id` 
                FROM 
                    #__vm_orders
                WHERE 
                    `user_id` = '{$userid}'
                        ");

                    $vmorderId = $database->loadResult();

        if ($database->query()) {
            $success = true; 
        }
    }       


    if (($already != $userid)) 
    {

$database->setQuery("

                SELECT 
                    `product_sku` 
                FROM 
                    #__vm_order_item
                WHERE 
                    `order_id` = '{$vmorderId}'
                        ");                     

                    $vmsku = $database->loadResult();

        if ($database->query()) {
            $success = true; 
        }
    }           



    // if they aren't already a member of the group, add them to the group
    if (($already != $userid)) 
    {
        $database->setQuery("
            UPDATE 
                #__xipt_users
            SET
                `profiletype` = '{$vmsku}'

            WHERE
                `userid` = '{$userid}'


            LIMIT 1

        ");

        if ($database->query()) {
            $success = true; 
        }
    } 

    return $success;
}

}

编辑:我现在按照建议尝试,使用JOIN完成任务,到目前为止没有快乐!

UPDATE

                        #__xipt_users

                    SET
                        `profiletype.#__xipt_users` = `product_sku.#__vmsku`

                    WHERE

                        `userid` = '{$userid}'


                    AND                         

                    ( 
                     SELECT `order_id.#__vm_orders`
                           FROM #__vm_orders, #__vm_order_item
                            LEFT JOIN #__vm_orders 
                            ON #__vm_orders.`order_id` = #__vm_order_item.`order_id` 
                            ORDER BY `order_id.#__vm_order` DESC LIMIT 1
                            WHERE
                            `user_id.#__vm_orders` = '{$userid}'

                    ) AS #__vmorder_id


                     SELECT ` product_sku.#__vm_order_item`
                           FROM #__vm_order_item, #__vmorder_id
                            LEFT JOIN #__vm_order_item 
                            ON `#__vm_order_item.order_id` = `#__vmorder_id.order_id` 
                            WHERE
                            `order_id.#__vm_order_item` = `order_id.#__vmorder_id`
                            )

                      AS #__vmsku

                    LIMIT 1

                    ");

解决方法:

加入声明
我建议你开始学习如何在MysqL中创建Join语句.

看看这个网站:
http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php

这样,您就可以将多个查询合并为一个.这将使这项工作变得更加容易!

一张纸
此外,它还可以帮助您在一张纸上绘制数据库,以便更好地了解您想要做什么.例如,您可以在要链接的表字段之间绘制线条.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐