specify conditions from outer query on a materialized subquery - mysql

i have got the below query which references couple of views 'goldedRunQueries' and 'currentGoldMarkings'. My issue seems to be from the view that is referred in the subquery - currentGoldMarkings. While execution, MySQL first materializes this subquery and then implements the where clauses of 'queryCode' and 'runId', which therefore results in execution time of more than hour as the view refers tables that has got millions of rows of data. My question is how do I enforce those two where conditions on the subquery before it materializes.
SELECT goldedRunQueries.queryCode, goldedRunQueries.runId
FROM goldedRunQueries
LEFT OUTER JOIN
( SELECT measuredRunId, queryCode, COUNT(resultId) as c
FROM currentGoldMarkings
GROUP BY measuredRunId, queryCode
) AS accuracy ON accuracy.measuredRunId = goldedRunQueries.runId
AND accuracy.queryCode = goldedRunQueries.queryCode
WHERE goldedRunQueries.queryCode IN ('CH001', 'CH002', 'CH003')
and goldedRunQueries.runid = 5000
ORDER BY goldedRunQueries.runId DESC, goldedRunQueries.queryCode;
Here are the two views. Both of these also get used in a standalone mode and so integrating any clauses into them is not possible.
CREATE VIEW currentGoldMarkings
AS
SELECT result.resultId, result.runId AS measuredRunId, result.documentId,
result.queryCode, result.queryValue AS measuredValue,
gold.queryValue AS goldValue,
CASE result.queryValue WHEN gold.queryValue THEN 1 ELSE 0 END AS correct
FROM results AS result
INNER JOIN gold ON gold.documentId = result.documentId
AND gold.queryCode = result.queryCode
WHERE gold.isCurrent = 1
CREATE VIEW goldedRunQueries
AS
SELECT runId, queryCode
FROM runQueries
WHERE EXISTS
( SELECT 1 AS Expr1
FROM runs
WHERE (runId = runQueries.runId)
AND (isManual = 0)
)
AND EXISTS
( SELECT 1 AS Expr1
FROM results
WHERE (runId = runQueries.runId)
AND (queryCode = runQueries.queryCode)
AND EXISTS
( SELECT 1 AS Expr1
FROM gold
WHERE (documentId = results.documentId)
AND (queryCode = results.queryCode)
)
)
Note: The above query reflects only a part of my actual query. There are 3 other left outer joins which are similar in nature to the above subquery which makes the problem far more worse.
EDIT: As suggested, here is the structure and some sample data for the tables
CREATE TABLE `results`(
`resultId` int auto_increment NOT NULL,
`runId` int NOT NULL,
`documentId` int NOT NULL,
`queryCode` char(5) NOT NULL,
`queryValue` char(1) NOT NULL,
`comment` varchar(255) NULL,
CONSTRAINT `PK_results` PRIMARY KEY
(
`resultId`
)
);
insert into results values (100, 242300, 'AC001', 'I', NULL)
insert into results values (100, 242300, 'AC001', 'S', NULL)
insert into results values (150, 242301, 'AC005', 'I', 'abc')
insert into results values (100, 242300, 'AC001', 'I', NULL)
insert into results values (109, 242301, 'PQ001', 'S', 'zzz')
insert into results values (400, 242400, 'DD006', 'I', NULL)
CREATE TABLE `gold`(
`goldId` int auto_increment NOT NULL,
`runDate` datetime NOT NULL,
`documentId` int NOT NULL,
`queryCode` char(5) NOT NULL,
`queryValue` char(1) NOT NULL,
`comment` varchar(255) NULL,
`isCurrent` tinyint(1) NOT NULL DEFAULT 0,
CONSTRAINT `PK_gold` PRIMARY KEY
(
`goldId`
)
);
insert into gold values ('2015-02-20 00:00:00', 138904, 'CH001', 'N', NULL, 1)
insert into gold values ('2015-05-20 00:00:00', 138904, 'CH001', 'N', 'aaa', 1)
insert into gold values ('2016-02-20 00:00:00', 138905, 'CH002', 'N', NULL, 0)
insert into gold values ('2015-12-12 00:00:00', 138804, 'CH001', 'N', 'zzzz', 1)
CREATE TABLE `runQueries`(
`runId` int NOT NULL,
`queryCode` char(5) NOT NULL,
CONSTRAINT `PK_runQueries` PRIMARY KEY
(
`runId`,
`queryCode`
)
);
insert into runQueries values (100, 'AC001')
insert into runQueries values (109, 'PQ001')
insert into runQueries values (400, 'DD006')
CREATE TABLE `runs`(
`runId` int auto_increment NOT NULL,
`runName` varchar(63) NOT NULL,
`isManual` tinyint(1) NOT NULL,
`runDate` datetime NOT NULL,
`comment` varchar(1023) NULL,
`folderName` varchar(63) NULL,
`documentSetId` int NOT NULL,
`pipelineVersion` varchar(50) NULL,
`isArchived` tinyint(1) NOT NULL DEFAULT 0,
`pipeline` varchar(50) NULL,
CONSTRAINT `PK_runs` PRIMARY KEY
(
`runId`
)
);
insert into runs values ('test1', 0, '2015-08-04 06:30:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)
insert into runs values ('test2', 1, '2015-12-04 12:30:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)
insert into runs values ('test3', 1, '2015-06-24 10:56:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)
insert into runs values ('test4', 1, '2016-05-04 11:30:46.000000', 'zzzz', '2015-08-04_103046', 2, '2015-08-03', 0, NULL)

First, let's try to improve the performance via indexes:
results: INDEX(runId, queryCode) -- in either order
gold: INDEX(documentId, query_code, isCurrent) -- in that order
After that, update the CREATE TABLEs in the question and add the output of:
EXPLAIN EXTENDED SELECT ...;
SHOW WARNINGS;
What version are you running? You effectively have FROM ( SELECT ... ) JOIN ( SELECT ... ). Before 5.6, neither subquery had an index; with 5.6, an index is generated on the fly.
It is a shame that the query is built that way, since you know which one to use: and goldedRunQueries.runid = 5000.
Bottom Line: add the indexes; upgrade to 5.6 or 5.7; if that is not enough, then rethink the use of VIEWs.

Related

MySQL query not acting as expected

I am trying to get all bills that have NOT been FULLY paid.
I have three tables that are needed for this.
Table 1 - billInvoiceMain
biId - unique ID
userId - users ID
type - bill or invoice
userItemId - unique ID that user chooses for their records
Table 2 - billInvoiceDetail
biId - references unique ID in billInvoiceMain
quantity
price
Table 3 - transaction
transactionId - unique ID
userId - users ID
biId - references id in billInvoiceMain
paymentAmount
So a user enters bills, and then once they make a payment (multiple smaller payments could be made on a bill until it reaches the full amount or they could make a single payment for the whole amount) they enter it and it gets saved in the transaction table.
Here is a SQL Fiddle that has abbreviated versions of test data.
CREATE TABLE IF NOT EXISTS `billInvoiceDetail` (
`biId` int(15) NOT NULL,
`productId` int(15) DEFAULT NULL,
`accountId` int(15) DEFAULT NULL,
`description` varchar(2000) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`quantity` decimal(20,3) NOT NULL,
`price` decimal(20,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `billInvoiceDetail` (`biId`, `productId`, `accountId`, `description`, `quantity`, `price`) VALUES
(51, NULL, 7, 'Pylaisiella steerei Ando & Higuchi', 4.000, 19.65),
(51, NULL, 11, 'Rubus insons L.H. Bailey', 1.000, 10.17),
(99, NULL, 11, 'Leontodon hispidus L.', 3.000, 11.99),
(99, NULL, 7, 'Peltophorum (T. Vogel) Benth.', 5.000, 33.76),
(100, NULL, 8, 'Scleria P.J. Bergius', 1.000, 10.55),
(100, NULL, 12, 'Gilia ochroleuca M.E. Jones ssp. exilis (A. Gray) A.D. Grant & V.E. Grant', 2.000, 42.54);
CREATE TABLE IF NOT EXISTS `billInvoiceMain` (
`biId` int(15) NOT NULL,
`userId` int(15) NOT NULL,
`type` varchar(7) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`cvId` int(15) NOT NULL,
`startDate` date DEFAULT NULL,
`dueDate` date DEFAULT NULL,
`userItemId` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `billInvoiceMain` (`biId`, `userId`, `type`, `cvId`, `startDate`, `dueDate`, `userItemId`) VALUES
(51, 1, 'bill', 17, '2021-01-01', '2021-01-31', '53396841'),
(99, 1, 'bill', 28, '2021-01-01', '2021-01-31', '16269083'),
(100, 1, 'bill', 28, '2021-01-07', '2021-01-17', '03200283');
CREATE TABLE IF NOT EXISTS `transaction` (
`transactionId` int(15) NOT NULL,
`userId` int(15) NOT NULL,
`biId` int(15) NOT NULL,
`paymentDate` date NOT NULL,
`paymentMethod` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`accountId` int(15) NOT NULL,
`paymentAmount` decimal(20,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `transaction` (`transactionId`, `userId`, `biId`, `paymentDate`, `paymentMethod`, `accountId`, `paymentAmount`) VALUES
(51, 1, 51, '2021-01-04', 'Check', 1, 78.60),
(52, 1, 51, '2021-01-19', 'Credit Card', 3, 10.17),
(53, 1, 99, '2021-01-14', 'Check', 1, 10.00);
SELECT billInvoiceMain.biId, SUM(transaction.paymentAmount), billInvoiceMain.useritemid
FROM billInvoiceMain
INNER JOIN transaction ON billInvoiceMain.biId = transaction.biId
WHERE billInvoiceMain.userId = 1 AND billInvoiceMain.type = 'bill'
GROUP BY billInvoiceMain.biId;
SELECT ROUND(ABS(SUM(billInvoiceDetail.price *billInvoiceDetail.quantity)),2)
FROM billInvoiceDetail
INNER JOIN billInvoiceMain ON billInvoiceDetail.biId = billInvoiceMain.biId
WHERE billInvoiceMain.userId=1 AND billInvoiceMain.type = 'bill'
GROUP BY billInvoiceMain.biId;
SELECT billInvoiceMain.biId, billInvoiceMain.useritemid
FROM billInvoiceMain
INNER JOIN transaction ON billInvoiceMain.biId = transaction.biId
INNER JOIN billInvoiceDetail ON billInvoiceDetail.biId = transaction.biId
WHERE billInvoiceMain.userId = 1 AND billInvoiceMain.type = 'bill'
HAVING SUM(transaction.paymentAmount) != ROUND(ABS(SUM(billInvoiceDetail.price *billInvoiceDetail.quantity)),2);
The first query allows me to sum of all the payments from transaction grouped by bill id.
The second query sums all the bills.
The third query I tried combing the two. However, when I try to use a GROUP BY, it gives an error. So, I got rid of that and now it just returns the first bill even if it has been paid.
Desired Results (retrieves the biId and userItemId of all bills that have not been fully paid based on the transaction table):
biId
userItemId
99
16269083
100
03200283
I have spent a lot of time trying to figure this out but am lost.
The following query retrieves rows that don't match the biId obtained by joining the result of sum of all the payments from transaction grouped by bill id and the result of sum of all the bills from billInvoiceDetail grouped by bill id.
SELECT biId, useritemid FROM billInvoiceMain
WHERE userId = 1 AND type = 'bill'
AND biId NOT IN(
SELECT t.biId FROM
(SELECT biId,SUM(paymentAmount) pay FROM transaction GROUP BY biId) t
INNER JOIN
(SELECT biId,ROUND(ABS(SUM(price*quantity)),2) bill FROM billInvoiceDetail GROUP BY biId) d
ON t.biId=d.biId AND t.pay=d.bill
)
SQL Fiddle

select last inserted row based on date

Main Problem Is:- select last inserted row based on date
i want to be able to select distinct ref row with the last created_At date.
this is my table and data
DROP TABLE IF EXISTS `transactions_logs`;
CREATE TABLE IF NOT EXISTS `transactions_logs` (
`trans_log_Id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`etat_de_commande` varchar(100) NOT NULL,
`ref` varchar(10) NOT NULL,
`commentaire` text NOT NULL,
`staffId` bigint(20) UNSIGNED NOT NULL,
`Created_At` datetime NOT NULL,
PRIMARY KEY (`trans_log_Id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
INSERT INTO `transactions_logs` (`trans_log_Id`, `etat_de_commande`, `ref`, `commentaire`, `staffId`, `Created_At`) VALUES
(1, 'waiting confirmation', '429735061', '', 1, '2020-11-09 12:11:43'),
(2, 'waiting confirmation', '472143970', '', 1, '2020-11-09 13:45:57'),
(3, 'confirmed', '429735061', '', 1, '2020-11-09 13:46:12'),
(4, 'ready', '429735061', '', 1, '2020-11-09 13:46:18'),
(5, 'picked', '429735061', '', 1, '2020-11-09 14:46:25');
COMMIT;
I want to be able to get this result
(2,'waiting confirmation','472143970',1,'2020-11-09 13:45:57'),
(5,'picked','429735061',1,'2020-11-09 14:46:25')
One option uses window functions, available in MySQL 8.0:
select *
from (
select t.*,
rank() over(partition by ref order by created_at desc) rn
from transactions_logs t
) t
where rn = 1
You can also use a correalted subquery for filtering - this works in all MySQL versions:
select t.*
from transactions_logs t
where t.created_at = (
select max(t1.created_at)
from transactions_logs t1
where t1.ref = t.ref
)
The latter would take advantage of an index on (ref, created_at).

MySQL UPDATE query partly updating with default values

I am running an update query (in MySQL 5.6) on 2 tables joined like the following:
UPDATE c_cache cc
JOIN p_cache pc USING (user_id, attribute_id, calculation_quarter)
JOIN batch_table bt USING (user_id, attribute_id, calculation_quarter, client_id, group_code, version_id)
SET cc.epop = SUBSTRING(bt.result, 1, 1),
cc.excl = SUBSTRING(bt.result, 2, 1),
cc.num_result = SUBSTRING(bt.result, 3, 20),
cc.status = 'FR',
pc.epop = IF(bt.enrolled = 2, SUBSTRING(bt.result, 1, 1), 1),
pc.excl = IF(bt.enrolled = 2, SUBSTRING(bt.result, 2, 1), 0),
pc.num_result = IF(bt.enrolled = 2, SUBSTRING(bt.result, 3, 20), REPEAT('0', 20)),
pc.status = IF(pc.status = 'FL2', 'S', 'FR');
The p_cache is being updated properly, but the 3 columns in the c_cache is being set to NULLs and status to 'S' (all default values of these columns).
The rows that are being missed out are usually contiguous (in chunks).
This query is within a loop in a stored procedure that runs till all 'S' (stale) status rows of p_cache are marked 'FR' (fresh), i.e. computed.
(All rows of p_cache are present in c_cache with a one-to-one correspondence).
The batch_table picks up rows in batches of 25000 rows per iteration, and gets updated with computed results in result column through some stored functions.
This whole stored proc. is called from a MySQL event. Multiple events run simultaneously (each for an exclusive set of attributes) to find stale rows in the p_cache, and update both cache tables with computed results in batches using queries similar to this one.
This anomalous behavior happens only on the c_cache, but only sometimes.
The schema definitions are:
CREATE TABLE c_cache (
user_id INT(11) NOT NULL DEFAULT '0',
attribute_id INT(11) NOT NULL DEFAULT '0',
calculation_quarter DATE NOT NULL DEFAULT '0000-00-00',
version_id INT(11) NOT NULL DEFAULT '0',
epop TINYINT(1) DEFAULT NULL,
excl TINYINT(1) DEFAULT NULL,
num_result CHAR(20) DEFAULT NULL,
status ENUM('FR','S','FL1','FL2') NOT NULL DEFAULT 'S',
PRIMARY KEY (user_id, attribute_id, calculation_quarter, version_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE p_cache (
user_id INT(11) NOT NULL DEFAULT '0',
attribute_id INT(11) NOT NULL DEFAULT '0',
calculation_quarter DATE NOT NULL DEFAULT '0000-00-00',
client_id INT(11) NOT NULL DEFAULT '0',
group_code CHAR(5) NOT NULL DEFAULT '',
epop TINYINT(1) DEFAULT NULL,
excl TINYINT(1) DEFAULT NULL,
num_result CHAR(20) DEFAULT NULL,
status ENUM('FR','S','FL1','FL2','S1','S2') NOT NULL DEFAULT 'S',
PRIMARY KEY (user_id,attribute_id,calculation_quarter,client_id,group_code),
KEY date_status_id_index (calculation_quarter,status,attribute_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Can anyone kindly explain why this is happening and suggest a way to avoid this?
Thanks in advance.

Mysql Correlated Query Error

I am trying to pair records according to four conditions, one of which requires a one-level correlated subquery to match a +/- date range condition.
Why is the old.stdt unknown in the subquery's where clause? Thank you.
Here are both data and query:
DROP TABLE IF EXISTS `sbids`;
CREATE TABLE `sbids` (
`bid` int(11) DEFAULT NULL,
`sid` int(11) DEFAULT NULL,
`ctle` varchar(20) DEFAULT NULL,
`stype` char(1) CHARACTER SET utf8 DEFAULT '',
`sbid` int(8) DEFAULT NULL,
`stdt` date DEFAULT NULL,
`sedt` date DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `sbids` VALUES
( 11228, 8367, 'RRR', 'A', 11495, '2012-02-01', '2014-01-01'),
( 11228, 8367, 'RRR', 'A', 8182, '2014-03-01', '2015-02-01'),
( 11228, 8367, 'CCC', 'B', 29256, '2014-09-01', '2015-08-31'),
( 11228, 18030, 'RRR', 'A', 23319, '2013-01-01', '2013-11-01'),
( 11228, 18030, 'CCC', 'A', 25304, '2014-01-01', '2014-11-01'),
( 11228, 18030, 'CCC', 'A', 25304, '2015-01-01', '2015-11-01');
drop table if exists prd;
create table prd as
select
old.bid,
old.sid,
old.sbid as old_sbid,
old.ctle as old_ctle,
old.stype as old_stype,
old.stdt as old_stdt,
old.exdt as old_exdt,
can.sbid as can_sbid,
can.ctle as can_ctle,
can.stype as can_stype,
can.stdt as can_stdt,
can.exdt as can_exdt
from
sbids old
inner join ( select
innerT.bid,
innerT.sid,
innerT.ctle,
min(innerT.stdt) as stdt
from
sbids innerT
where
abs(datediff( old.stdt , innerT.exdt )) <= 65
group by
innerT.bid,
innerT.sid,
innerT.ctle ) can
on old.bid = can.bid
and old.sid = can.sid
and old.ctle = can.ctle

issues with mysql GROUP_CONCAT and INSERT into another table

I have a MySQL GROUP_CONCAT .... INSERT issue
This code works:
SELECT group_concat(tabel2.img_name separator ',')
FROM tabel2
GROUP BY tabel2.produit_id
I need to insert the result into another table and I'm stuck.
This (or any combination I could think of) doesn't work
INSERT INTO tabel1.imgname
SELECT group_concat(tabel2.img_name separator ',')
FROM tabel2
GROUP BY tabel2.produit_id
WHERE tabel1.product_id = tabel2.produit_id
What am I doing wrong?
CREATE TABLE IF NOT EXISTS `tabel1` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`rubrique_id` int(11) NOT NULL,
`marque_id` int(11) NOT NULL,
`subfamily_id` int(11) NOT NULL,
`product_name` varchar(150) NOT NULL,
`imgname` varchar(255) DEFAULT NULL,
`product_description1` text NOT NULL,
`product_description2` text NOT NULL,
`product_order` int(11) NOT NULL,
`product_page` int(11) NOT NULL,
`price_min` float NOT NULL,
PRIMARY KEY (`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;
INSERT INTO `tabel1` (`product_id`, `rubrique_id`, `marque_id`, `subfamily_id`, `product_name`, `imgname`, `product_description1`, `product_description2`, `product_order`, `product_page`, `price_min`)
VALUES
(33, 15, 23, 40, 'product 1', NULL, '', '', 0, 0, 0),
(34, 13, 13, 13, 'product 2', NULL, '', '', 0, 0, 0),
(35, 14, 14, 14, 'product 3', NULL, '', '', 0, 0, 0);
CREATE TABLE IF NOT EXISTS `tabel2` (
`img_id` int(11) NOT NULL AUTO_INCREMENT,
`img_name` text NOT NULL,
`article_id` int(11) DEFAULT NULL,
`produit_id` int(11) DEFAULT NULL,
`product_select` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`img_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4018 ;
INSERT INTO `tabel2` (`img_id`, `img_name`, `article_id`, `produit_id`, `product_select`)
VALUES
(4013, 'acoacki086050.jpg', 13342, 33, NULL),
(4014, '32252il6jh2dqex.jpg', NULL, 34, NULL),
(4015, '33265ulrzmgr18w.jpg', NULL, 34, NULL),
(4016, '40334zcfk0c4n67.jpg', NULL, 35, NULL),
(4017, '40473frd4900u82.jpg', NULL, 35, NULL);
So for say tabel1.product_id = 34 I need to have tabel1.imgname = 32252il6jh2dqex.jpg,33265ulrzmgr18w.jpg
I think this is what you are trying to do:
INSERT INTO tabel1 (product_id,imgname)
SELECT tabel2.produit_id, group_concat(tabel2.img_name separator ',') as imgname
FROM tabel2
GROUP BY tabel2.produit_id
Update: Thanks for posting the schema and clarifying the requirements.
This should work for you:
UPDATE tabel1
INNER JOIN
(
SELECT tabel2.produit_id, group_concat(tabel2.img_name separator ',') AS imgname
FROM tabel2
GROUP BY tabel2.produit_id
) s ON s.produit_id = tabel1.product_id
SET tabel1.imgname = s.imgname;
What you describe is UPDATE. not INSERT:
UPDATE
tabel1 AS t1
JOIN
( SELECT produit_id
, GROUP_CONCAT(img_name SEPARATOR ',') AS grp_img_name
FROM tabel2
GROUP BY produit_id
) AS t2
ON t2.produit_id = t1.product_id
SET
t1.imgname = t2.grp_img_name ;
Random rants:
Why, why, why do you have these table1, tabel1, tabel2, tableX names? Names of any object (of tables, columns, indexes, constraints, databases) should reflect usage.
table1 says nothing.
tabel1 says nothing except that you don't spell-check your names.
It's good practice that columns used in Foreign Key constraints (and as a result for joining), have identical names (if possible). Not imgname in one table and img_name in another. Not produit_id in one and product_id in the other. It helps you, the next programmer and the guys who try to help you over SO, to not make mistakes writing ON t2.produit_id = t1.produit_id. It can also help, if you like the USING (product_id) syntax instead of the ON syntax for joins.
(update)
It seems that the different names are because the tables come from different sources/databases and it was not your choice. I would suggest you alter them to have uniform/sensible names, if you have a lot of work to do with them. If it's a one-time job, just to transfer/transform some data, don't bother.
You did not specify a value to insert
INSERT INTO tabel1.imgname
SELECT group_concat(tabel2.img_name separator ',')
FROM tabel2
GROUP BY tabel2.produit_id
WHERE tabel1.product_id = tabel2.produit_id
VALUES('','')
etc