简体   繁体   English

mysql从表中选择,具体取决于数据在哪个表中

[英]mysql select from a table depending on in which table the data is in

I have 3 tables holding products for a restaurant. 我有3张桌子,上面放着一家餐厅的产品。 Products that reside in the bar, food and ingredients. 酒吧内的产品,食物和配料。

I use php and mysql. 我使用php和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). 有2个字段(最重要的字段)保存有关产品ID和类型的信息(来自酒吧,厨房或配料)。 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. 我当时想编写如下的sql查询,以便将该表用于酒吧产品,厨房或配料,但它不起作用。 Basically the second table on join must be either "bar", "produse" or "stoc". 基本上,第二个连接表必须是“ bar”,“ produse”或“ 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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM