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

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)