dputlf5431 2010-03-31 15:40
浏览 22

mysql从表中选择,具体取决于数据所在的表

I have 3 tables holding products for a restaurant. Products that reside in the bar, food and ingredients.

I use php and mysql. I have another table that holds information about the orders that have been made so far. There are 2 fields, the most important ones, that hold information about the id of the product and the type (from the bar, from the kitchen or from the ingredients). I was thinking to write the sql query like below to use either the table for bar products, kitchen or ingredients but it doesn't work. Basically the second table on join must be either "bar", "produse" or "stoc".

SELECT K.nume, 
       COUNT(K.cantitate) as cantitate, 
       SUM(K.pret) as pret, 
       P.nume as NumeProduse 
  FROM `clienti_fideli` as K 
  JOIN if(K.tip,bar,produse) AS P ON K.produs = P.id_prod 
 WHERE K.masa=18 and K.nume LIKE 'livrari-la-domiciliu' 
GROUP BY NumeProduse
  • 写回答

1条回答 默认 最新

  • dongmei1911 2010-03-31 15:50
    关注

    I don't think you can do that.

    can you change the database schema? a better option would be to have bar, food and ingredients in a single table (eg product) with a field type which would be either 'bar', 'produse' or 'stoc'

    SELECT K.nume, 
           COUNT(K.cantitate) as cantitate, 
           SUM(K.pret) as pret, 
           P.nume as NumeProduse 
      FROM `clienti_fideli` as K 
      JOIN product AS P ON K.produs = P.id_prod  AND P.tip = <product_type>
     WHERE K.masa=18 and K.nume LIKE 'livrari-la-domiciliu' 
    GROUP BY NumeProduse
    
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗