1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `tab1`
--------------
--------------
CREATE TABLE IF NOT EXISTS `tab1`
( `id` integer unsigned not null auto_increment primary key,
`produit` varchar(255) not null,
`prix_achat` decimal(15,2) not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `tab1` (`produit`,`prix_achat`) values
('produit 1', 50), ('produit 2', 20), ('produit 1', 45),('produit 1', 55)
--------------
--------------
select * from tab1
--------------
+----+-----------+------------+
| id | produit | prix_achat |
+----+-----------+------------+
| 1 | produit 1 | 50.00 |
| 2 | produit 2 | 20.00 |
| 3 | produit 1 | 45.00 |
| 4 | produit 1 | 55.00 |
+----+-----------+------------+
--------------
DROP TABLE IF EXISTS `tab2`
--------------
--------------
CREATE TABLE IF NOT EXISTS `tab2`
( `id` integer unsigned not null auto_increment primary key,
`produit` varchar(255) not null,
`prix_vente` decimal(15,2) not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `tab2` (`produit`,`prix_vente`) values
('produit 1', 55), ('produit 2', 29), ('produit 1', 51),('produit 1', 62)
--------------
--------------
select * from tab2
--------------
+----+-----------+------------+
| id | produit | prix_vente |
+----+-----------+------------+
| 1 | produit 1 | 55.00 |
| 2 | produit 2 | 29.00 |
| 3 | produit 1 | 51.00 |
| 4 | produit 1 | 62.00 |
+----+-----------+------------+
--------------
select produit,
sum(prix_achat) as prix_achat,
null as prix_vente
from tab1
group by produit
union
select produit,
null as prix_achat,
sum(prix_vente) as prix_vente
from tab2
group by produit
--------------
+-----------+------------+------------+
| produit | prix_achat | prix_vente |
+-----------+------------+------------+
| produit 1 | 150.00 | NULL |
| produit 2 | 20.00 | NULL |
| produit 1 | NULL | 168.00 |
| produit 2 | NULL | 29.00 |
+-----------+------------+------------+
--------------
select produit,
max(prix_achat) as prix_achat,
max(prix_vente) as prix_vente
from (
select produit,
sum(prix_achat) as prix_achat,
null as prix_vente
from tab1
group by produit
union
select produit,
null as prix_achat,
sum(prix_vente) as prix_vente
from tab2
group by produit
) as x
group by produit
order by produit
--------------
+-----------+------------+------------+
| produit | prix_achat | prix_vente |
+-----------+------------+------------+
| produit 1 | 150.00 | 168.00 |
| produit 2 | 20.00 | 29.00 |
+-----------+------------+------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager