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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条