dongqie4402 2011-06-07 11:35
浏览 52
已采纳

php mysql保存查询结果

i have a query that goes like this:

            $get_feed_amount = mysql_query("

                SELECT COUNT(*) as num
                FROM `migo_feeds`
                WHERE
                (
                    (
                        feed_type='10' ||
                        feed_type='11' ||
                        feed_type='28' ||
                        feed_type='27' ||
                        feed_type='30' ||
                        feed_type='31'

                    )
                    AND
                    (
                        from_id IN (

                            SELECT DISTINCT from_id
                            FROM `migo_relations`
                            WHERE (
                                (to_id='".$_SESSION['user_id']."')

                                AND

                                (rel_processed='1' AND rel_accepted='1')
                            )                       

                        )

                        OR

                        to_id IN (

                            SELECT DISTINCT from_id
                            FROM `migo_relations`
                            WHERE (
                                (to_id='".$_SESSION['user_id']."')

                                AND

                                (rel_processed='1' AND rel_accepted='1')
                            )                               

                        )

                        OR

                        from_id=".$_SESSION['user_id']."

                        OR

                        to_id=".$_SESSION['user_id']."

                    )
                    AND
                    feed_deleted='0'
                );

            "); 

as you can see im allready selecting friends twice in this query

and also later i do queries like this:

            $migo_groups = mysql_query("

                SELECT 
                    group_id,
                    group_title,
                    group_desc,
                    group_cat_name,
                    group_area,
                    group_city,
                    group_quarter,
                    group_pic,
                    group_tmb,
                    group_members_amount
                FROM
                    `migo_groups`
                WHERE (

                    group_type=1

                    AND

                    group_id IN (

                        SELECT group_id
                        FROM `migo_group_membership`
                        WHERE (

                            membership_type IN (1,2,3)

                            AND

                            to_id IN (

                                SELECT DISTINCT from_id
                                FROM `migo_relations`
                                WHERE (
                                    (to_id='".$_SESSION['user_id']."')

                                    AND

                                    (rel_processed='1' AND rel_accepted='1')
                                )                                   
                            )
                        )
                    )
                )
                ORDER BY RAND() LIMIT 4;
            ");

selecting friends once again.. this seems a little bit of waste of resources. Especially if the tables are very large, things will probably take forever to load, or not even load at all.

My question is, can i just do a friend query in the beginning and use the result everywhere? Is that any good at all?

I was thinking perhaps save the resutls in an array or something, is that bad? What would be the best way to run things?

Regards, Alexander

  • 写回答

1条回答 默认 最新

  • douhu2370 2011-06-07 14:01
    关注

    You can save the results of the first query in an array and then using only the array for your second need.

    Example:

    $result=array();
    $query=mysql_query("SELECT complex sql [...]");
    while($r = mysql_fetch_assoc($query)) {
        $result[] = $r;
        /*your first operation here*/
    }
    

    Now in your result you have all the results of the first query, and you can use that array to do your second operation without the need of other queries.

    foreach($result as $row) {
          // code
    }
    

    Also note you should use PDO.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题