2017-11-03 13 views
1

商品のリストがあります。在庫があるかどうかを確認したいと思います。注文を隠す必要がないかどうかを表示するために、すべてのコードが表示されている場合。すべての商品が表示されている場合または表示されていない場合は、

私は2つのテーブルを持っています。 1つは顧客注文の製品、もう1つは店舗に在庫がある製品です。

私は、このMySql接続ですべての商品がある注文のみを表示します。私はすべての製品を持っていない場合、リスト内の順序を非表示にします。

PHP

<?php while($row = $sql->fetch_array()){?> 
    <tr id="row<?php echo $row['id'];?>" style="height:20px"> 
     <td><?php echo $row['id_comanda']; ?></td> 
     <td><?php echo $row['nume_produs']; ?></td> 
     <td><?php echo $row['qty']; ?></td> 
     <td><?php echo $row['stoc']; ?></td> 
    </tr> 
<?php } $DBcon->close();?> 

のMySQL

SELECT c.id_comanda 
    ,c.nume_produs 
    ,c.qty 
    ,s.sku 
    ,SUM(s.stoc) as stoc 
    FROM comenzi_tel_prod c 
INNER JOIN stocuri_mentor s ON c.nume_produs = s.sku AND s.stoc >= c.qty 
GROUP BY c.nume_produs 


OR 

SELECT c.* 
    ,s.* 
    ,SUM(stoc) AS stoc 
FROM comenzi_tel_prod c 
INNER JOIN stocuri_mentor s ON c.nume_produs = s.sku 
    AND stoc >= c.qty 
GROUP BY c.id_comanda 

データベース:

comenzi_tel_prod 

+----+------------+-------------+-----+-------+------------+----------------------------------------+--------------+----------+ 
| id | id_comanda | nume_produs | qty | price | pret_engro |    nume_ales    | furnizor | subtotal | 
+----+------------+-------------+-----+-------+------------+----------------------------------------+--------------+----------+ 
| 94 |   18 | K316546  | 2 | 17.85 |  16.16 | SUPER NET PURPLE 38cm x 38cm, EMGO, EA | Parts Europe | 523.719 | 
| 95 |   18 | K316539  | 1 | 14.67 |  13.28 | SUPER NET-BLACK 38cm x 38cm, EMGO, EA | Parts Europe | 14.67 | 
+----+------------+-------------+-----+-------+------------+----------------------------------------+--------------+----------+ 



    stocuri_mentor 

+----+---------+---------------------------------------+--------+------+---------------+------------+-----------+-----------+-------+--------------------+--------------+-----+---------------+ 
| id | sku |     nume     | den_um | stoc | stoc_rezervat | pret_engro | sim_clasa | den_clasa | brand |  furnizor  | pret_vanzare | gol |  ean  | 
+----+---------+---------------------------------------+--------+------+---------------+------------+-----------+-----------+-------+--------------------+--------------+-----+---------------+ 
| 1 | K316539 | Arc ambreaj pornire Piaggio Ciao/Si | Buc | 1 |    0 |  0,87 | CIF  | CIF  | CIF | MOTO BIKE SHOP SRL |   1,04 |  | 6686245374874 | 
| 2 | K316546 | Paleta racire variator Piaggio/Gilera | Buc | 1 |    0 |  35,64 | CIF  | CIF  | CIF | MOTO BIKE SHOP SRL |  42,41 |  | 6686245375017 | 
| 3 | 11503 | Cui ponto scuter carburator PHBN/PHVA | Buc | 0 |    0 |  23,23 | CIF  | CIF  | Cif | MOTO BIKE SHOP SRL |  27,64 |  | 6686245375093 | 
| 4 | 11503 | Cui ponto scuter carburator PHBN/PHVA | Buc | 1 |    0 |  23,23 | CIF  | CIF  | Cif | MOTO BIKE SHOP SRL |  27,64 |  | 6686245375093 | 
| 5 | 0531z11 | Pinion z11 Garelli     | Buc | 1 |    0 |  24,5 | CIF  | CIF  | CIF | MOTO BIKE SHOP SRL |  29,16 |  | 6686245375246 | 
| 6 | 11507 | Filtru holender Piaggio    | Buc | 3 |    0 |  9,11 | CIF  | CIF  | CIF | MOTO BIKE SHOP SRL |  10,84 |  | 6686245470590 | 
+----+---------+---------------------------------------+--------+------+---------------+------------+-----------+-----------+-------+--------------------+--------------+-----+---------------+ 

答えて

0

この

`SELECT c.* 
     ,s.* 
     ,count(c.id) as instock_count 
     ,totals.orders_total 

    FROM comenzi_tel_prod c 
    INNER JOIN (SELECT *, sum(stoc) as stoc_sum 
       FROM stocuri_mentor GROUP BY 
       sku) s 
    ON c.nume_produs = s.sku 
    INNER JOIN (SELECT count(id) as orders_total, id_comanda 
       FROM comenzi_tel_prod 
       GROUP BY id_comanda) totals 
    ON totals.id_comanda = c.id_comanda 

    WHERE stoc_sum >= c.qty 
    GROUP BY c.id_comanda 
    HAVING instock_count = orders_total` 
+1

を試してみて、我々は、Cをした後にそれは素晴らしい作品hanged *、SUM(stoc)をstoc_sumとして返します。 –

+0

あなたのために働いてうれしい! :) –

関連する問題