duanrou5680 2016-01-20 17:57
浏览 82
已采纳

从CSV文件中获取值并搜索MySQL数据库

I have a query a Wordpress include which does the following:

        $sql = "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'merchant_id' LIMIT 6";
$results = $wpdb->get_results($sql);
     foreach ($results as $row)
                {
                    echo $row->meta_value . ",";
                    //sprintf("SELECT * FROM retailers WHERE advertiserId = '%s'", $row->meta_value);
                }

    //clean all results
    $wpdb->flush();

It parses all pages custom fields (merchant ID numbers), and returns any unique distinct values, separated by a comma. This bit works great.

ie: The above may return: 1301,345,723,134,1435

What I also have is a separate MySQL table of about 20 fields, three of which are the MerchantID, programmeName, and commissionMax. Each value from the CSV correlates with the MerchantID in the database.

Regardless of the amount of merchant ID's that appear in the CSV - I need to parse them all, but show the three highest commission rates (commissionMax), as well as the programmeName.

I managed to get connected to my external database and retrieve the appropriate values (using the commented code above) however this showed all of the retailers information.

Any advice?

  • 写回答

1条回答 默认 最新

  • drk7700 2016-01-20 18:03
    关注

    Use the following query with limit:

    SELECT *                             // select all fields
    FROM table_name                      // from your table
    WHERE MerchantID IN (your_ids_here)  // IDs received from previous query or wherever
    ORDER BY commissionMax DESC          // descending sort by commissionMax field
    LIMIT 3                              // take first 3 results
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮