I would like to merge a table that the data comes from 2 differents databases.
I operated as described below:
1 – I have done a dump of the source database table and I get the following insert query:
INSERT INTO `t_vaccination` VALUES (242,NULL,NULL,53,1,'20030528','0','W5770-2',0,'DTP - REVAXIS','A 20130521170623','2013-05-21 17:06:23'),
(243,NULL,NULL,53,1,'20130525','0','',1,'DTP - ','A 20130521170623','2013-05-21 17:06:23'),
(1830,NULL,NULL,50,1,'20080502','3','',0,'DTP - REVAXIS','A 20130521170623','2013-05-21 17:06:23'),
(1831,NULL,NULL,50,1,'20130501','4','',1,'DTP - ','A 20130521170623','2013-05-21 17:06:23'),
(1832,NULL,NULL,50,1,'20080502','3','',0,'PAPILLOMAVIRUS - Gardasil','A 20130521170623','2013-05-21 17:06:23')
the structure of the t_vaccination table is:
CREATE TABLE `t_vaccination` (
`nIdVaccination` INT(10) UNSIGNED NOT NULL,
`nIdVaccin` INT(10) UNSIGNED NULL DEFAULT NULL,
`nIdVacProtocole` INT(10) UNSIGNED NULL DEFAULT NULL,
`nIdPatient` INT(10) UNSIGNED NOT NULL,
`nIdUtilisateur` INT(10) UNSIGNED NULL DEFAULT NULL,
`sDateInjection` VARCHAR(8) NOT NULL DEFAULT '',
`nNumInjection` VARCHAR(45) NOT NULL DEFAULT '0',
`sNumLot` VARCHAR(45) NOT NULL DEFAULT '',
`nRappel` TINYINT(4) NOT NULL DEFAULT '0',
`sLibelle` VARCHAR(255) NOT NULL DEFAULT '',
`sAction` VARCHAR(16) NOT NULL DEFAULT 'A 20080101000000',
`sDH_REPLIC` DATETIME NULL DEFAULT '2010-01-01 00:00:00',
PRIMARY KEY (`nIdVaccination`),
INDEX `NDX_t_vaccination_nIdUtilisateur` (`nIdUtilisateur`),
INDEX `NDX_t_vaccination_nIdVaccin` (`nIdVaccin`),
INDEX `NDX_t_vaccination_nIdVacProtocole` (`nIdVacProtocole`),
INDEX `NDX_t_vaccination_nIdPatient` (`nIdPatient`),
CONSTRAINT `FK_vaccination_nIdUtilisateur_utilisateur` FOREIGN KEY (`nIdUtilisateur`) REFERENCES `t_utilisateur` (`nIdUtilisateur`),
CONSTRAINT `FK_vaccination_nIdVaccin_vaccin` FOREIGN KEY (`nIdVaccin`) REFERENCES `t_vaccin` (`nIdVaccin`)
)
2 - I would like to insert all the rows in the t_vaccination table of the final database without inserting the duplicates rows. the new query run by inserting one row:
INSERT INTO t_vaccination (nIdVaccination, nIdVaccin, nIdVacProtocole, nIdPatient, nIdUtilisateur, sDateInjection, nNumInjection, sNumLot, nRappel, sLibelle, sAction, sDH_REPLIC)
SELECT 251,41,4,53,1,'20030528','0','W5770-2',0,'DTP - REVAXIS','A 20130521170623','2013-05-21 17:06:23' FROM t_vaccination WHERE NOT EXISTS (SELECT nIdVaccin, nIdVacProtocole, nIdPatient, nIdUtilisateur FROM t_vaccination WHERE nIdVaccin = 41 and nIdVacProtocole = 4 and nIdPatient = 53 and nIdUtilisateur =1 ) LIMIT 1
3 - Is it possible to insert rows by group by using insert where not exists because the attempts that i have done failed. here is an example of an an insert that failed:
INSERT INTO t_vaccination (nIdVaccination, nIdVaccin, nIdVacProtocole, nIdPatient, nIdUtilisateur, sDateInjection, nNumInjection, sNumLot, nRappel, sLibelle, sAction, sDH_REPLIC)
SELECT 251,41,4,53,1,'20030528','0','W5770-2',0,'DTP - REVAXIS','A 20130521170623','2013-05-21 17:06:23' FROM t_vaccination WHERE NOT EXISTS (SELECT nIdVaccin, nIdVacProtocole, nIdPatient, nIdUtilisateur FROM t_vaccination WHERE nIdVaccin = 41 and nIdVacProtocole = 4 and nIdPatient = 53 and nIdUtilisateur =1 ) LIMIT 1,
SELECT 243,NULL,NULL,53,1,'20130525','0','',1,'DTP - ','A 20130521170623','2013-05-21 17:06:23' FROM t_vaccination WHERE NOT EXISTS (SELECT nIdVaccin, nIdVacProtocole, nIdPatient, nIdUtilisateur FROM t_vaccination WHERE nIdVaccin = NULL and nIdVacProtocole = NULL and nIdPatient = 53 and nIdUtilisateur =1 ) LIMIT 1
I hope for your help.
Regards
Motti
In my opinion, the simplest way to do what you want is to remove Unique keys/indexes and remove dupes or create a temporary table without those keys. Let's assume you create a temp_t_vaccination table and import all your rows in, you'll then just have to do :
INSERT INTO t_vaccination (field1, field2 ...) SELECT DISTINCT field1, fields2 ... FROM temp_vaccination
ref : http://dev.mysql.com/doc/refman/5.0/en/insert-select.html?ff=nopfpls
Related
I am exposing my concern to you.
There are "demande_psy" and "consultant" tables. Each "demande_psy" record can relate to several "consultants" records, and each "consultant" record can relate to several "demande_psy" records, according to a "statut". This is the reason why, the primary key of the "demande_psy_consultant" link table (many to many relation table) is composed of: "id_demande_psy", "id_consultant", "statut". By querying the database , I need to retrieve a list of demande_psy and their priority statut, according to these 3 rules:
If among the same demand_psy, I have at least one AFFECTED status => my request line must display the "Affected" status
Otherwise, if among the same demande_psy, I have at least one status ACCEPTED => my demande_psy line must display "Accepted"
Otherwise my demande_psy line should show "Sent"
Here my tables creation code :
CREATE TABLE `demande_psy` (
`id` int(10) UNSIGNED NOT NULL,
`titre` varchar(255) NOT NULL,
`detail` text NOT NULL,
`sms` text NOT NULL,
`created_at` datetime NOT NULL,
`modified_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `demande_psy` (`id`, `titre`, `detail`, `sms`, `created_at`)
VALUES (1, 'Test demande', 'bla bla bla bla', 'qsdf\r\nqsdf\r\n', '2021-03-18 17:23:59');
ALTER TABLE `demande_psy` ADD PRIMARY KEY (`id`);
ALTER TABLE `demande_psy` MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
CREATE TABLE `consultant` (
`id` int(11) UNSIGNED NOT NULL,
`prenom` varchar(100) DEFAULT NULL,
`nom` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `consultant` (`id`, `prenom`, `nom`) VALUES
(52, 'Michel', 'Moral (Somica - Undici)'),
(55, 'Patrick', 'Amar');
ALTER TABLE `consultant` ADD PRIMARY KEY (`id`);
ALTER TABLE `consultant` MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=56;
CREATE TABLE `demande_psy_consultant` (
`id_demande_psy` int(10) UNSIGNED NOT NULL,
`id_consultant` int(10) UNSIGNED NOT NULL,
`statut` enum('SENT','ACCEPTED','AFFECTED') NOT NULL,
`created_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `demande_psy_consultant` (`id_demande_psy`, `id_consultant`,
`statut`, `created_at`) VALUES
(1, 52, 'SENT', '2021-03-18 17:39:59'),
(1, 55, 'ACCEPTED', '2021-03-18 19:05:45');
ALTER TABLE `demande_psy_consultant` ADD PRIMARY KEY (`id_demande_psy`,`id_consultant`,`statut`);
ALTER TABLE `demande_psy_consultant`
ADD CONSTRAINT `fk_consultant` FOREIGN KEY (`id_consultant`) REFERENCES `consultant` (`id`),
ADD CONSTRAINT `fk_demande_psy` FOREIGN KEY (`id_demande_psy`) REFERENCES `demande_psy` (`id`);
Here a simplified example :
demande_psy
-----------
1 toto
6 titi
consultant
----------
15 Marc
88 Jean
demande_psy_consultant
----------------------
1 15 SENT
1 62 SENT
1 88 ACCEPTED
1 88 AFFECTED
6 88 SENT
6 15 SENT
==> EXPECTED RESULT
---------------------------------------------------
1 88 AFFECTED (according to the 3 rules)
6 15 SENT
And now, here is my sql query:
SELECT
DPC.id_demande_psy,
DPC.id_consultant,
(CASE
WHEN (SELECT count(DPC.statut) FROM demande_psy_consultant DPC WHERE DPC.statut = "AFFECTED")>0 THEN "AFFECTED"
WHEN (SELECT count(DPC.statut) FROM demande_psy_consultant DPC WHERE DPC.statut = "ACCEPTED")>0 THEN "ACCEPTED"
ELSE "SENT"
END) as statutComputed
FROM demande_psy_consultant DPC
GROUP BY DPC.id_demande_psy
With it, I obtain :
MY (BAD) RESULT
---------------
1 15 AFFECTED
6 15 AFFECTED
So I struggle a bit to correct it to obtain the desired result If anyone has an idea, I'm interested
thank you !
So I have a few tables on mySQL:
CREATE TABLE IF NOT EXISTS `salarygrade` (
`GRADE` INT(11) NOT NULL,
`HOURLYRATE` FLOAT NOT NULL,
PRIMARY KEY (`GRADE`));
===========================================================================
CREATE TABLE IF NOT EXISTS `staffongrade` (
`STAFFNO` INT(11) NOT NULL,
`GRADE` INT(11) NOT NULL,
`STARTDATE` DATE NULL DEFAULT NULL,
`FINISHDATE` DATE NULL DEFAULT NULL,
INDEX `STAFFONGRADE_FK` (`STAFFNO` ASC),
INDEX `STAFFONGRADE2_FK` (`GRADE` ASC),
PRIMARY KEY (`GRADE`, `STAFFNO`),
CONSTRAINT `FK_STAFFONG_STAFFONGR_SALARYGR`
FOREIGN KEY (`GRADE`)
REFERENCES `salarygrade` (`GRADE`),
CONSTRAINT `FK_STAFFONG_STAFFONGR_STAFF`
FOREIGN KEY (`STAFFNO`)
REFERENCES `staff` (`STAFFNO`));
===========================================================================
CREATE TABLE IF NOT EXISTS `campaign` (
`CAMPAIGN_NO` INT(11) NOT NULL,
`TITLE` VARCHAR(30) NOT NULL,
`CUSTOMER_ID` INT(11) NOT NULL,
`THEME` VARCHAR(40) NULL DEFAULT NULL,
`CAMPAIGNSTARTDATE` DATE NULL DEFAULT NULL,
`CAMPAIGNFINISHDATE` DATE NULL DEFAULT NULL,
`ESTIMATEDCOST` INT(11) NULL DEFAULT NULL,
`ACTUALCOST` FLOAT NULL DEFAULT NULL,
PRIMARY KEY (`CAMPAIGN_NO`),
INDEX `OWNS_FK` (`CUSTOMER_ID` ASC),
CONSTRAINT `FK_CAMPAIGN_OWNS_CUSTOMER`
FOREIGN KEY (`CUSTOMER_ID`)
REFERENCES `customer` (`CUSTOMER_ID`)
ON DELETE RESTRICT
ON UPDATE RESTRICT);
===========================================================================
CREATE TABLE IF NOT EXISTS `workson` (
`STAFFNO` INT(11) NOT NULL,
`CAMPAIGN_NO` INT(11) NOT NULL,
`WDATE` DATE NOT NULL,
`HOUR` FLOAT NULL DEFAULT NULL,
PRIMARY KEY (`STAFFNO`, `CAMPAIGN_NO`, `WDATE`),
INDEX `WORKSON_FK` (`STAFFNO` ASC),
INDEX `FK_WORKSON_WORKSON2_CAMPAIGN_idx` (`CAMPAIGN_NO` ASC),
CONSTRAINT `FK_WORKSON_WORKSON2_CAMPAIGN`
FOREIGN KEY (`CAMPAIGN_NO`)
REFERENCES `campaign` (`CAMPAIGN_NO`)
ON DELETE RESTRICT
ON UPDATE RESTRICT,
CONSTRAINT `FK_WORKSON_WORKSON_STAFF`
FOREIGN KEY (`STAFFNO`)
REFERENCES `staff` (`STAFFNO`));
And I want to create a stored procedure called sp_finish_campaign (in c_title varchar(30)) that takes a title of a campaign and finishes the campaign by updating the CAMPAIGNFINISHDATE to the current date and ACTUALCOST to the cost of the campaign, which is calculated from the number of hours different staff put into it on different dates, and the salary grade (this changes based on staffID and the timeframe based on the STARTDATE and FINISHDATE of the staffongrade table.
To calculate the ACTUALCOST, I created a helper function:
DELIMITER //
CREATE FUNCTION rate_on_date(staff_id int, given_date date)
RETURNS int
DETERMINISTIC
BEGIN
DECLARE salaryGrade int;
SET salaryGrade = (select grade from staffongrade
where staffno = staff_id AND (given_date BETWEEN STARTDATE AND FINISHDATE));
RETURN salaryGrade;
END //
DELIMITER ;
Which returns the pay grade based on the staff_id and given_date parameters I give it:
select rate_on_date(1, "2018-02-02") as Grade_On_Date;
For the parameters of this I assume I would have to get it from the workson table which looks like this:
I have tried using a select statement to get the paygrade:
select hourlyrate as 'grade' from salarygrade
where rate_on_date(1, "2018-02-02") = grade;
To calculate ACTUALCOST I assume I would have to do a calculation by multiplying HOUR column with the grade costs, and use the WDATE and STAFFNO columns in the workson table as parameters for my stored procedure that will calculate and update the CAMPAIGNFINISHDATE and ACTUALCOST of the campaign by inputting the campaign title into it.
But how would I go about doing this?
I'm just confused as to how to go about creating this procedure, and also confused about how to properly use these helper functions in my stored procedure.
I feel like this question is quite long but I don't really know what to ask or what direction I should take to solve this problem.
You don't really need a function. mysql can do multi-table updates (see https://dev.mysql.com/doc/refman/8.0/en/update.html) in your case it could look like this
update campaign c
join
(select c.campaign_no,
sum(hour * hourlyrate) cost
from campaign c
join workson w on w.campaign_no = c.campaign_no
join staffongrade s on s .staffno = w.staffno and w.wdate between s.startdate and s.finishdate
join salarygrade g on g.grade = s.grade
group by c.campaign_no
) s
on s.campaign_no = c.campaign_no
set actualcost = s.cost
where c.campaign_no = 1
;
Where the sub query does the needful
if you simplify your data this should be easy to prove;
drop table if exists salarygrade,campaign,workson,staffongrade;
CREATE TABLE `salarygrade`
( GRADE INT NOT NULL,
hOURLYRATE decimal(10,2) NOT NULL
);
insert into salarygrade values(1,10),(2,20);
cREATE TABLE IF NOT EXISTS `staffongrade` (
`STAFFNO` INT(11) NOT NULL,
`GRADE` INT(11) NOT NULL,
`STARTDATE` DATE NULL DEFAULT NULL,
`FINISHDATE` DATE NULL DEFAULT NULL
);
insert into staffongrade values
(1,1,'2019-01-01','2019-06-30'),(1,2,'2019-06-01','2019-12-31'),(2,1,'2019-01-01','2019-01-31');
CREATE TABLE IF NOT EXISTS `campaign` (
`CAMPAIGN_NO` INT(11) NOT NULL,
`CAMPAIGNSTARTDATE` DATE NULL DEFAULT NULL,
`CAMPAIGNFINISHDATE` DATE NULL DEFAULT NULL,
`ESTIMATEDCOST` INT(11) NULL DEFAULT NULL,
`ACTUALCOST` FLOAT NULL DEFAULT NULL
);
insert into campaign values (1,'2019-01-01','2019-12-31',null,null);
CREATE TABLE IF NOT EXISTS `workson` (
`STAFFNO` INT(11) NOT NULL,
`CAMPAIGN_NO` INT(11) NOT NULL,
`WDATE` DATE NOT NULL,
`HOUR` FLOAT NULL DEFAULT NULL
);
insert into workson values
(1,1,'2019-01-01',1),(1,1,'2019-12-01',1),(2,1,'2019-01-01',1);
select * from campaign;
+-------------+-------------------+--------------------+---------------+------------+
| CAMPAIGN_NO | CAMPAIGNSTARTDATE | CAMPAIGNFINISHDATE | ESTIMATEDCOST | ACTUALCOST |
+-------------+-------------------+--------------------+---------------+------------+
| 1 | 2019-01-01 | 2019-12-31 | NULL | 40 |
+-------------+-------------------+--------------------+---------------+------------+
1 row in set (0.00 sec)
Got to dash so I'll leave you to drop the update into a procedure.
IF staffongrade has NULL for finishdate then a bit of data cleansing is required. for simplicity I would create a temporary table to fill in gaps and change the update statement to use the projectfinishdate (if that's not known then substitute a suitable future date). This code would be inserted in your procedure prior to the update
so
insert into staffongrade values
(1,1,'2019-01-01',null),(1,2,'2019-07-01',null),(2,1,'2019-01-01',null);
drop temporary table if exists staffongradetemp;
create temporary table staffongradetemp like staffongrade;
insert into staffongradetemp
select s.STAFFNO,s.GRADE,s.STARTDATE,
case when s.FINISHDATE is not null then s.finishdate
else date_sub((select s1.startdate
from staffongrade s1
where s1.STAFFNO = s.STAFFNO and s1.startdate > s.STARTDATE
order by startdate limit 1), interval 1 day)
end
from staffongrade s
;
select * from staffongradetemp;
+---------+-------+------------+------------+
| STAFFNO | GRADE | STARTDATE | FINISHDATE |
+---------+-------+------------+------------+
| 1 | 1 | 2019-01-01 | 2019-06-30 |
| 1 | 2 | 2019-07-01 | NULL |
| 2 | 1 | 2019-01-01 | NULL |
+---------+-------+------------+------------+
3 rows in set (0.00 sec)
Which leaves all the last finshdates as null which we can trap in the update statement using coalesce
update campaign c
join
(select c.campaign_no,
sum(hour * hourlyrate) cost
from campaign c
join workson w on w.campaign_no = c.campaign_no
join **staffongradetemp s** on s .staffno = w.staffno and w.wdate between s.startdate and **coalesce(s.finishdate,c.CAMPAIGNFINISHDATE)**
join salarygrade g on g.grade = s.grade
where c.campaign_no = 1
group by c.campaign_no
) s
on s.campaign_no = c.campaign_no
set actualcost = s.cost
where 1 = 1;
Can anyone help me understand what this code does?
CREATE TABLE `exams` (
`id` int(11) NOT NULL, `score` int(2) NOT NULL, `class` int(11) NOT NULL, `date` date NOT NULL, PRIMARY KEY (`class`,`date`,`id`));
CREATE TRIGGER `trig3` BEFORE INSERT ON `exams` FOR EACH ROW SET #xxx = #xxx + 1;
SET #xxx = 0;
INSERT INTO `exams` VALUES (10001,24,1,'2013-09-16'), (10005,30,1,'2013-09-16'), (10083,30,1,'2014-03-21'), (10120,26,1,'2014-03-21'), (10035,23,1,'2014-07-22'), (10005,28,2,'2014-01-23'), (10001,27,2,'2014-06-30'), (10001,25,4,'2014-01-23'), (10083,22,4,'2014-01-23'), (10120,30,4,'2014-01-23'), (10005,22,4,'2014-03-21');
SELECT #xxx;
I understand the creation of the table and the insertion of values but i don't get the rest of it.
The code seams to be a workaround way to count or check if the multi insert inserted all records or not.
SELECT #xxx;
| #xxx |
| ---- |
| 11 |
But MySQL doesn't have a native way to find that out.
see demo
A more easy MySQL code to do the same without needing the TRIGGER code and SET.
But it will require a new table structure to function.
CREATE TABLE `esami` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY
, `id_studente` int(11) NOT NULL
, `voto` int(2) NOT NULL
, `id_corso` int(11) NOT NULL
, `data` date NOT NULL
, UNIQUE KEY (`id_corso`,`data`,`id_studente`)
);
And the query to test if all records where inserted by the multi insert.
SELECT
COUNT(*)
FROM
esami
WHERE
id BETWEEN LAST_INSERT_ID() AND LAST_INSERT_ID() + 11;
see demo
I have a mysql table:
CREATE TABLE `templates_assignments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`type` int(11) NOT NULL DEFAULT '2'
`assignment_id` int(11) NOT NULL DEFAULT '1',
`template_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `template_id` (`template_id`),
) ENGINE=InnoDB AUTO_INCREMENT=122 DEFAULT CHARSET=latin1;
/*Data for the table `templates_assignments` */
insert into `templates_assignments`
(`id`,`type`,`assignment_id`,`template_id`)
values
(15,1,1,1),
(16,1,1,2),
(19,1,1,6),
(54,2,30,6),
(55,1,5,11),
(56,1,5,15),
(57,1,5,22);
I want to select the template that qualifies for both conditions:
type=2 AND assignment_id=30
type=1 AND assignment_id=1
the only template_id that should come back is 6, but i keep getting all or none.
My query condition was something like:
WHERE
(
(templatesAssignments.type=2 AND templatesAssignments.assignment_id=30) AND (templatesAssignments.type=1 AND templatesAssignments.assignment_id=1)
)
But no luck...what am i missing?
SELECT ta1.template_id
FROM templatesAssignments ta1
INNER JOIN templatesAssignments ta2 ON ta1.template_id = ta2.template_id
WHERE (ta1.type=1 AND ta1.assignment_id=1)
AND (ta2.type=2 AND ta2.assignment_id=30)
select template_id
from templatesAssignments
group by template_id
having sum(type=2 AND assignment_id=30) > 0
and sum(type=1 AND assignment_id=1) > 0
Why I get "Copying to tmp table on disk" after several iteration of loop? In the first few iterations I have not this. Table has 10 million and more rows. When I set lover offset, loop execute more iteration without "tmp table".
My select is
SELECT u.domena_id, u.umiestnenie, u.datum
FROM (SELECT domena_id, min(datum) as min_datum
FROM umiestnenie
WHERE datum BETWEEN 'date1' AND 'date2'
GROUP BY domena_id
LIMIT 200000
OFFSET offset (increasing in lopp)
) x
INNER JOIN umiestnenie u ON u.domena_id = x.domena_id and u.datum = x.min_datum
Table umiestnenie
CREATE TABLE IF NOT EXISTS `umiestnenie` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`domena_id` int(11) NOT NULL,
`datum` date NOT NULL,
`umiestnenie` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_umiestnenie_domeny_idx` (`domena_id`),
KEY `datum_idx` (`datum`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15243077 ;
Table domena
CREATE TABLE IF NOT EXISTS `domena` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nazov` varchar(200) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `nazov` (`nazov`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15343156 ;
Why when I run this select one time, it doesn't need temporary table and when I run this select more times it does?
This is Explain of my select
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 1000000
1 PRIMARY u ref domena_id_idx domena_id_idx 4 x.domena_id 7 Using where
2 DERIVED umiestnenie index NULL domena_id_idx 4 NULL 20109031 Using where
Here is not tmp table, but when I run this select with increased offset from php I see using of temp table.