duansan9435 2012-04-11 08:28
浏览 39
已采纳

mysql选择查询以预填充下拉字段

As part of my ecommerce application, I have this interesting problem, I've been trying to resolve.

I have two things.

  • Category
  • Product

In relational database sense, a category can have more than one products, and a product can only belong to category.

Then, I have this ecommerce admin coupon page where I want to modify the coupon details that's associated with a particular product and category.

On the modify page, I have the following fields

Coupon description - TextField type Coupon Price - TextField type Coupon Percentage - TextField type Category Name - Dropdownfield type Product Name - Dropdownfield type

I have the following sql query.

$sql = "SELECT cp_description, cp_discountprice, cp_discountpercent, pd_name, cat_name
        FROM tbl_coupon inner join (tbl_product, tbl_category) on 
        (tbl_product.pd_id = tbl_coupon.pd_id AND     tbl_category.cat_id=tbl_product.cat_id)
        WHERE cp_id = $cpId";

What's really interesting about this problem is that I could not include pd_id and cat_id fields into the sql query so I can manipulate them in another sql code that will look up these fields to retrieve correct selected element in a dropdown field when prepopulated.

I cannot use pd_name or cat_name in select statement because these fields are not 'unique' so can cause problems.

Does anybody have any idea what's the best way to approach this problem? I thought the distinct keyword may do the trick... But it doesn't!

  • 写回答

1条回答 默认 最新

  • douke7274 2012-04-11 08:35
    关注

    Can't you use a query like this?

    SELECT 
        cp.cp_description, cp.cp_discountprice, cp.cp_discountpercent, 
        p.pd_id, p.pd_name, 
        cat.cat_id, cat.cat_name
    FROM tbl_coupon cp 
    INNER JOIN tbl_product p ON p.pd_id = cp.pd_id
    INNER JOIN tbl_category cat ON p.cat_id = cat.cat_id 
    WHERE cp_id = $cpId
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题