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

从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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题