SQL UPDATE where in JOIN Clause - mysql

I have two tables: tt_news and tt_news_cat.
tt_news hast the columns tx_newscalendar_calendardate and a relation field for the category called category
the table tt_news_cat has an uid. The relation is an m:m relation. m:m is written in the table tt_news_cat_mm with thre fields uid_local (the news uid) and uid_foreign (the categories uid).
I would like to update a field in tt_news where the correesponding category uid is 12. My Update Clause in phpmyadmin looks as follows:
UPDATE b
SET `tx_newscalendar_calendardate` = `datetime`
FROM `tt_news` AS b
INNER JOIN `tt_news_cat` AS i
WHERE `i.category` = 12
What I am doing wrong here?
Thanks.
PS: table definitions:
#
# Table structure for table 'tt_news'
#
CREATE TABLE tt_news (
uid int(11) NOT NULL auto_increment,
pid int(11) DEFAULT '0' NOT NULL,
tstamp int(11) unsigned DEFAULT '0' NOT NULL,
crdate int(11) unsigned DEFAULT '0' NOT NULL,
cruser_id int(11) unsigned DEFAULT '0' NOT NULL,
editlock tinyint(4) unsigned DEFAULT '0' NOT NULL,
deleted tinyint(3) unsigned DEFAULT '0' NOT NULL,
hidden tinyint(4) unsigned DEFAULT '0' NOT NULL,
starttime int(11) unsigned DEFAULT '0' NOT NULL,
endtime int(11) unsigned DEFAULT '0' NOT NULL,
fe_group varchar(100) DEFAULT '0' NOT NULL,
title text,
datetime int(11) unsigned DEFAULT '0' NOT NULL,
image text,
imagecaption text,
imagealttext text,
imagetitletext text,
related int(11) DEFAULT '0' NOT NULL,
short text,
bodytext mediumtext,
author varchar(255) DEFAULT '' NOT NULL,
author_email varchar(255) DEFAULT '' NOT NULL,
category int(11) DEFAULT '0' NOT NULL,
news_files text,
links text,
type tinyint(4) DEFAULT '0' NOT NULL,
page int(11) DEFAULT '0' NOT NULL,
keywords text,
archivedate int(11) DEFAULT '0' NOT NULL,
ext_url varchar(255) DEFAULT '' NOT NULL,
sys_language_uid int(11) DEFAULT '0' NOT NULL,
l18n_parent int(11) DEFAULT '0' NOT NULL,
l18n_diffsource mediumblob NOT NULL,
no_auto_pb tinyint(4) unsigned DEFAULT '0' NOT NULL,
t3ver_oid int(11) DEFAULT '0' NOT NULL,
t3ver_id int(11) DEFAULT '0' NOT NULL,
t3ver_wsid int(11) DEFAULT '0' NOT NULL,
t3ver_label varchar(30) DEFAULT '' NOT NULL,
t3ver_state tinyint(4) DEFAULT '0' NOT NULL,
t3ver_stage tinyint(4) DEFAULT '0' NOT NULL,
t3ver_count int(11) DEFAULT '0' NOT NULL,
t3ver_tstamp int(11) DEFAULT '0' NOT NULL,
t3_origuid int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (uid),
KEY parent (pid),
KEY t3ver_oid (t3ver_oid,t3ver_wsid),
KEY datetime (datetime)
);
#
# Table structure for table 'tt_news_cat'
#
CREATE TABLE tt_news_cat (
uid int(11) NOT NULL auto_increment,
pid int(11) DEFAULT '0' NOT NULL,
tstamp int(11) unsigned DEFAULT '0' NOT NULL,
crdate int(11) unsigned DEFAULT '0' NOT NULL,
hidden tinyint(4) unsigned DEFAULT '0' NOT NULL,
starttime int(11) unsigned DEFAULT '0' NOT NULL,
endtime int(11) unsigned DEFAULT '0' NOT NULL,
sorting int(11) unsigned DEFAULT '0' NOT NULL,
fe_group varchar(100) DEFAULT '0' NOT NULL,
title varchar(255) DEFAULT '' NOT NULL,
title_lang_ol varchar(255) DEFAULT '' NOT NULL,
image varchar(255) DEFAULT '' NOT NULL,
shortcut int(11) unsigned DEFAULT '0' NOT NULL,
shortcut_target varchar(255) DEFAULT '' NOT NULL,
deleted tinyint(3) unsigned DEFAULT '0' NOT NULL,
single_pid int(11) unsigned DEFAULT '0' NOT NULL,
parent_category int(11) unsigned DEFAULT '0' NOT NULL,
description text,
PRIMARY KEY (uid),
KEY parent (pid),
KEY parent_category (parent_category)
);
#
# Table structure for table 'tt_news_related_mm'
#
CREATE TABLE tt_news_related_mm (
uid_local int(11) DEFAULT '0' NOT NULL,
uid_foreign int(11) DEFAULT '0' NOT NULL,
sorting int(11) DEFAULT '0' NOT NULL,
tablenames varchar(255) DEFAULT '' NOT NULL,
KEY uid_local (uid_local),
KEY uid_foreign (uid_foreign)
);
#
# Table structure for table 'tt_news_cat_mm'
#
CREATE TABLE tt_news_cat_mm (
uid_local int(11) DEFAULT '0' NOT NULL,
uid_foreign int(11) DEFAULT '0' NOT NULL,
tablenames varchar(30) DEFAULT '' NOT NULL,
sorting int(11) DEFAULT '0' NOT NULL,
KEY uid_local (uid_local),
KEY uid_foreign (uid_foreign)
);

Related

GROUP BY retrieve from the history table the last event

I have 3 tables and I want to retrieve from the history table the last event, so I wrote this request but the result is not good. or am i mistaken? thank you
CREATE TABLE `equipment` (
`id_equipment` int(10) UNSIGNED NOT NULL,
`reference` varchar(32) DEFAULT NULL COMMENT 'clé AIRO2',
`buying_date` date DEFAULT NULL,
`first_use` date DEFAULT NULL,
`checking_delai` varchar(45) DEFAULT NULL,
`serial_number` varchar(256) DEFAULT NULL,
`lot_number` varchar(256) DEFAULT NULL,
`lapsing_date` date NOT NULL,
`status` varchar(32) NOT NULL,
`inspection` date NOT NULL,
`compteur` int(11) DEFAULT NULL,
`id_model` int(10) UNSIGNED DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`detruit` int(1) DEFAULT NULL,
`date_REEPREUVE` date DEFAULT NULL,
`validite_apragaz` date DEFAULT NULL,
`restituee_AJR` int(11) NOT NULL,
`perdu` int(11) DEFAULT NULL,
`maintenance_catt_o2` date DEFAULT NULL,
`vendu` int(11) DEFAULT NULL,
`colonne_inogen` date DEFAULT NULL COMMENT 'date changement colonne inogen',
`rappel_525KS_dec19` int(1) DEFAULT NULL COMMENT 'rappel_cable_DEVILBISS_525_KS'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='matériel';
CREATE TABLE `equipment_history` (
`id_equipment_history` int(10) UNSIGNED NOT NULL,
`id_user` int(10) UNSIGNED NOT NULL,
`id_equipment` int(10) UNSIGNED DEFAULT NULL,
`id_patient` int(11) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
`status_date` datetime DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`tuyau` tinyint(1) DEFAULT NULL,
`cable_alim` tinyint(1) DEFAULT NULL,
`compteur` int(11) DEFAULT NULL,
`verif_alarme` tinyint(1) DEFAULT NULL,
`verif_volume` tinyint(1) DEFAULT NULL,
`verif_pression` tinyint(1) DEFAULT NULL,
`verif_o2` tinyint(1) DEFAULT NULL,
`verif_debit` tinyint(1) DEFAULT NULL,
`remplacer_consommable` tinyint(1) DEFAULT NULL,
`complet` tinyint(1) DEFAULT NULL,
`comment_panne` text,
`ras` tinyint(1) DEFAULT NULL,
`panne` tinyint(1) DEFAULT NULL,
`id_piece_jointe` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='historique de l''utilisation du matériel';
CREATE TABLE `patient_has_equipment` (
`id_patient_has_equipment` int(10) UNSIGNED NOT NULL,
`id_intervention` int(10) UNSIGNED DEFAULT NULL,
`id_equipment` int(10) UNSIGNED NOT NULL,
`id_patient` int(10) UNSIGNED DEFAULT NULL,
`date_attribution` date DEFAULT NULL,
`id_user` int(11) DEFAULT NULL,
`version_appli` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
request :
SELECT
equipment.id_equipment,
equipment.reference,
history.updated,
history.`status`
FROM
equipment
LEFT JOIN
(
SELECT
*
FROM
`equipment_history`
GROUP BY
id_equipment
ORDER BY
`equipment_history`.`updated`
DESC
) AS history
ON
history.id_equipment = equipment.id_equipment
WHERE
history.`status` = 3 AND equipment.id_equipment NOT IN(
SELECT
id_equipment
FROM
patient_has_equipment
)
thanks
The group by subquery is the problem. While this is a syntax error in most databases, MySQL - unfortunately - tolerates this syntax (when sql mode ONLY_FULL_GROUP_BY is disabled), but does not do what you think: you actually an arbitary record from the table for each equipment_id (the order by clause has no effect on this behavior).
You need to filter for the last history record rather than aggregate. I think that this should do what you want:
select
e.id_equipment,
e.reference,
eh.updated,
eh.status
from equipment e
inner join equipment_history eh on eh.id_equipment = e.id_equipment
where
eh.status = 3
and eh.updated = (
select max(eh1.updated)
from equipment_history eh1
where eh1.id_equipment = e.id_equipment
)
and not exists (
select 1
from patient_has_equipment phe
where phe.id_equipment = e.id_equipment
)

I can't login to my e-shop based on Prestashop 1.7. (No ps_employee table in DB.)

I can't find ps_employee table in the DB.
Is it possible that this table has just dissappeard?
How can I create new one and add new admin account?
Of course is not possible this table can disappear by itself, anyway you can create it together with the associated _shop table:
CREATE TABLE IF NOT EXISTS `ps_employee` (
`id_employee` int(10) unsigned NOT NULL auto_increment,
`id_profile` int(10) unsigned NOT NULL,
`id_lang` int(10) unsigned NOT NULL DEFAULT '0',
`lastname` varchar(32) NOT NULL,
`firstname` varchar(32) NOT NULL,
`email` varchar(128) NOT NULL,
`passwd` varchar(60) NOT NULL,
`last_passwd_gen` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`stats_date_from` date DEFAULT NULL,
`stats_date_to` date DEFAULT NULL,
`stats_compare_from` date DEFAULT NULL,
`stats_compare_to` date DEFAULT NULL,
`stats_compare_option` int(1) unsigned NOT NULL DEFAULT 1,
`preselect_date_range` varchar(32) DEFAULT NULL,
`bo_color` varchar(32) DEFAULT NULL,
`bo_theme` varchar(32) DEFAULT NULL,
`bo_css` varchar(64) DEFAULT NULL,
`default_tab` int(10) unsigned NOT NULL DEFAULT '0',
`bo_width` int(10) unsigned NOT NULL DEFAULT '0',
`bo_menu` tinyint(1) NOT NULL DEFAULT '1',
`active` tinyint(1) unsigned NOT NULL DEFAULT '0',
`optin` tinyint(1) unsigned NOT NULL DEFAULT '1',
`id_last_order` int(10) unsigned NOT NULL DEFAULT '0',
`id_last_customer_message` int(10) unsigned NOT NULL DEFAULT '0',
`id_last_customer` int(10) unsigned NOT NULL DEFAULT '0',
`last_connection_date` date DEFAULT NULL,
`reset_password_token` varchar(40) DEFAULT NULL,
`reset_password_validity` datetime DEFAULT NULL,
PRIMARY KEY (`id_employee`),
KEY `employee_login` (`email`,`passwd`),
KEY `id_employee_passwd` (`id_employee`,`passwd`),
KEY `id_profile` (`id_profile`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION;
CREATE TABLE IF NOT EXISTS `ps_employee_shop` (
`id_employee` INT(11) UNSIGNED NOT NULL,
`id_shop` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id_employee`,`id_shop`),
KEY `id_shop` (`id_shop`)
) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8 COLLATION;
Now only need add a record with your account details, in the passwd you can add whatever text, because you will need recover your password in the admin login page.

#1060 - Duplicate column name 'ID' Why?

CREATE TABLE IF NOT EXISTS `vehicles` (
`UID` int(4) NOT NULL,
`id` int(11) NOT NULL,
`Kofferraum` varchar(50) NOT NULL DEFAULT '0|0|0|0|',
`Typ` int(11) NOT NULL,
`Tuning` varchar(255) NOT NULL,
`Spawnpos_X` varchar(50) NOT NULL,
`Spawnpos_Y` varchar(50) NOT NULL,
`Spawnpos_Z` varchar(50) NOT NULL,
`Spawnrot_X` varchar(50) NOT NULL,
`Spawnrot_Y` varchar(50) NOT NULL,
`Spawnrot_Z` varchar(50) NOT NULL,
`Farbe` varchar(50) NOT NULL,
`Paintjob` varchar(50) NOT NULL DEFAULT '3',
`Benzin` varchar(50) NOT NULL DEFAULT '100',
`Slot` float NOT NULL,
`Special` int(11) NOT NULL DEFAULT '0',
`Lights` varchar(50) NOT NULL DEFAULT '|255|255|255|',
`Distance` double NOT NULL DEFAULT '0',
`STuning` varchar(50) NOT NULL DEFAULT '0|0|0|0|0|0|',
`AuktionsID` int(10) NOT NULL DEFAULT '0',
`GangVehicle` tinyint(1) NOT NULL DEFAULT '0',
`rc` int(1) NOT NULL DEFAULT '0',
`spezcolor` varchar(50) NOT NULL DEFAULT '|0|0|0|0|0|0|',
`Sportmotor` int(1) NOT NULL DEFAULT '0',
`Bremse` varchar(1) NOT NULL DEFAULT '0',
`Antrieb` varchar(10) NOT NULL,
`plate` text NOT NULL,
`ID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
Because you're using 'id' twice. Either remove the duplicate or change the name of this to something else:
ID int(11) NOT NULL
You have the column id twice. Once at the beginning an one at the end.
CREATE TABLE IF NOT EXISTS `vehicles` (
`UID` int(4) NOT NULL,
`id` int(11) NOT NULL,
...
`ID` int(11) NOT NULL,
PRIMARY KEY (`ID`)
...
remove one of them

Mysql Match…against query performance in InnoDB Mysql 5.6

I need to search records in the table which contains millions of records. I have recently updated Mysql version from 5.1 to 5.6.
I was using like in query which was taking around 15 sec to 30 sec.
Currently I have modified query to use MATCH... AGAINST feature of mysql 5.6 INNODB. My query goes like this.
SELECT JOB.IDJOB, JOB.IDEMPLOYER .....
FROM JOB
WHERE ( ( JOB.ITJOBSTARTTYPE=2 AND JOB.DTPLANNEDEND >= '2015-07-06' )
OR ( JOB.ITJOBSTARTTYPE=1
AND ( ( JOB.ITJOBENDTYPE=2 ) OR ( JOB.DTJOBEND>='2015-07-06') )
)
OR ( JOB.ITJOBSTARTTYPE=3
AND ( (JOB.DTJOBEND >='2015-07-06') OR (JOB.ITJOBENDTYPE=2) )
)
)
AND MATCH(VCJOBTITLE, LVJOBCOMPANYDESCRIPTION, VCCOMPANYNAME,
VCSALARYDESC, VCCITY, VCQUALIFICATIONREQUIRED,VCJOBREFERENCE,
LVJOBKEYWORDS )
AGAINST ('test' IN NATURAL LANGUAGE MODE)
ORDER BY JOB.ITJOBBAND ASC,
JOB.VCRANKING DESC,
JOB.IDJOB DESC,
JOB.FJOBWEIGHT ASC
LIMIT 50;
Which takes 80- 120 seconds (3X slower )
If I remove ORDER BY it will load in 6 to 10 sec.
Is their any setting Or performance tunning we can apply here?
Edit: using show create table JOB;
CREATE TABLE `JOB` (
`idJob` int(11) NOT NULL AUTO_INCREMENT,
`idEmployer` int(10) DEFAULT NULL,
`vcJobTitle` varchar(255) NOT NULL,
`lvJobCompanyDescription` text,
`idBasket` int(10) DEFAULT NULL,
`dtActualgolive` datetime DEFAULT NULL COMMENT 'whenever job status become online',
`boImmediatelygolive` int(11) NOT NULL,
`dtRequestedgolive` date DEFAULT NULL,
`dtActualend` datetime DEFAULT NULL COMMENT 'when job is set to be archieved',
`dtPlannedend` date NOT NULL COMMENT 'when posting a job this is calculated. batch will look at this date. date on which regular jobs will be archievd, or date on which internship jobs will be changed to listing',
`dtRequestedend` date DEFAULT NULL COMMENT 'exact date on which employer wants jobs to be archieved',
`dtApplicationdeadline` date DEFAULT NULL COMMENT 'job will be converted to listing on this date',
`boDurationinweeks` int(11) NOT NULL COMMENT 'whether employer gave duration in weeks or not',
`itDurationweeks` int(2) DEFAULT NULL,
`boIsoncredit` int(11) NOT NULL,
`dtCreditend` date DEFAULT NULL,
`fJobbaseprice` float NOT NULL,
`fJobpriceafteradminitemdiscount` float NOT NULL COMMENT 'price after admin item discount',
`fJobpriceafterpromodiscount` float NOT NULL COMMENT 'price after promo discount',
`vcPromotioncode` varchar(50) DEFAULT NULL,
`vcCompanyname` varchar(100) NOT NULL,
`vcSalarydesc` varchar(255) NOT NULL,
`vcCity` varchar(100) NOT NULL,
`vcCounty` varchar(120) DEFAULT NULL,
`vcQualificationrequired` text,
`boWorkfromhome` int(11) NOT NULL,
`boResidential` int(11) NOT NULL,
`boIndoor` int(11) NOT NULL,
`boOutdoor` int(11) NOT NULL,
`boIndoorandoutdoor` int(11) NOT NULL,
`itJobstarttype` int(11) DEFAULT NULL COMMENT 'date,immidiate,always recruiting',
`dtJobstart` date DEFAULT NULL,
`itJobendtype` int(11) DEFAULT NULL COMMENT 'date,ongoing',
`dtJobend` date DEFAULT NULL,
`dtFeaturedstart` date DEFAULT NULL,
`dtFeaturedend` date DEFAULT NULL,
`itFulltimeparttime` int(11) NOT NULL COMMENT 'fulltime,parttime,both',
`boEveningtime` int(11) NOT NULL,
`boDaytime` int(11) NOT NULL,
`boWeekend` int(11) NOT NULL,
`boNewsletter` int(11) NOT NULL,
`vcApplyemail` varchar(500) DEFAULT NULL,
`vcApplyphone` varchar(200) DEFAULT NULL,
`vcApplyaddress` varchar(100) DEFAULT NULL,
`vcApplyURL` varchar(500) DEFAULT NULL,
`boRequirephone` int(11) DEFAULT NULL,
`boRequireaddress` int(11) DEFAULT NULL,
`boRequirecv` int(11) DEFAULT NULL,
`boFeatured` int(11) NOT NULL,
`blLogo` longblob,
`itJobstatus` int(1) DEFAULT NULL COMMENT 'online,offline',
`itEmailedtofriendcount` int(5) DEFAULT NULL COMMENT 'keeps count of how many times users clicked on link email this to friend',
`itDeadlinetype` int(1) DEFAULT NULL,
`idInternshiptypemaster` int(11) DEFAULT NULL,
`vcLengthofscheme` varchar(100) DEFAULT NULL,
`dtInternshiplistingend` date DEFAULT NULL COMMENT 'date when internship job will archieve',
`itJobband` int(2) DEFAULT NULL,
`boGraduate` int(11) NOT NULL,
`boInternship` int(11) NOT NULL,
`boGaptemp` int(11) NOT NULL,
`boParttimeholiday` int(11) NOT NULL,
`boEntrylevel` int(11) NOT NULL DEFAULT '0',
`boHundredPercentDiscountApplicable` int(11) NOT NULL,
`vcContactdetails` varchar(100) DEFAULT NULL,
`vcJobreference` varchar(100) DEFAULT NULL COMMENT 'for importing totaljobs feeds',
`vcJoburlparam` varchar(100) DEFAULT NULL COMMENT 'for importing totaljobs url params',
`dtPutInCurrentBand` datetime DEFAULT NULL COMMENT 'date when job is online and put in band 3',
`itInitcountycount` int(2) NOT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`lvJobkeywords` text,
`fJobweight` decimal(10,2) DEFAULT '1.00' COMMENT 'Holds the job weight ranging between 0 to 1 (1 being the highest weight)',
`dtReinstatedOn` datetime DEFAULT NULL,
`boIncludeiniframe` int(11) DEFAULT NULL,
`boIsAdvertFeatured` int(11) DEFAULT NULL,
`itincludexml` int(1) DEFAULT '1',
`boAddtojobalert` int(11) DEFAULT '1',
`vcRanking` int(11) DEFAULT '999999',
`itIncludeUkptj` int(11) DEFAULT '1',
PRIMARY KEY (`idJob`),
KEY `JOB_I_1` (`idEmployer`,`itJobstatus`),
KEY `JOB_I_2` (`itJobstatus`,`boGaptemp`,`dtJobstart`),
KEY `JOB_I_3` (`itJobstatus`,`boGraduate`,`dtJobstart`),
KEY `JOB_I_4` (`itJobstatus`,`boInternship`,`dtJobstart`),
KEY `JOB_I_5` (`itJobstatus`,`boParttimeholiday`,`dtJobstart`),
KEY `FI_JOB_idOrder_ORDER_idOrder` (`idBasket`),
KEY `FI_JOB_vcPromotioncode_PROMOTION_vcPromotioncode` (`vcPromotioncode`),
KEY `FI_JOB_idInternshiptype_ITTM_idInternshiptypemaster` (`idInternshiptypemaster`),
KEY `JOB_I_6` (`created_at`),
KEY `boEntrylevel` (`boEntrylevel`),
KEY `itJobband` (`itJobband`),
FULLTEXT KEY `index_ft_search` (`vcJobTitle`,`lvJobCompanyDescription`,`vcCompanyname`,`vcSalarydesc`,`vcCity`,`vcQualificationrequired`,`vcJobreference`,`lvJobkeywords`),
CONSTRAINT `fk_JOB_idEmployer_EMPLOYER_idEmployer` FOREIGN KEY (`idEmployer`) REFERENCES `EMPLOYER` (`idEmployer`),
CONSTRAINT `fk_JOB_idInternshiptype_ITTM_idInternshiptypemaster` FOREIGN KEY (`idInternshiptypemaster`) REFERENCES `INTERNSHIPTYPEMASTER` (`idInternshiptypemaster`),
CONSTRAINT `fk_JOB_idOrder_ORDER_idOrder` FOREIGN KEY (`idBasket`) REFERENCES `BASKET` (`idBasket`),
CONSTRAINT `fk_JOB_vcPromotioncode_PROMOTION_vcPromotioncode` FOREIGN KEY (`vcPromotioncode`) REFERENCES `PROMOTION` (`vcPromotioncode`)
) ENGINE=InnoDB AUTO_INCREMENT=1739324 DEFAULT CHARSET=latin1 |
Edit:
Without sort:
With Sort:

n-number of nested inner join

I have a sql-statement which correctly fetches data from one table. However, I need to fetch in the same way from n number of tables named table_n. All of these tables contain a 3-field primary key of projid, docid and revnr. I need to return n as docType as well, to differentiate the tables. The result will be sorted by projid and/or docid.
I tried sorting all the outputs from the different queries in PHP but it was way too slow (at least a few seconds on a 3MB database). I'm convinced MySQL/MSSQL will do it faster.
This is my current query:
SELECT a.* FROM `table_1` a
INNER JOIN (SELECT docid,
Max(revnr) max_val
FROM `table_1`
WHERE ( projid = something )
GROUP BY docid) b
ON a.docid = b.docid
AND a.revnr = b.max_val ORDER BY docid DESC
My current query gets the rows with highest revnr for each docid and projid.
I'm developing on MySQL but I need it to work on MSSQL as well. A general SQL solution would be great.
Thanks!
EDIT: Table schemas of the tables i currently have:
CREATE TABLE IF NOT EXISTS `table_1` (
`projid` int(11) NOT NULL,
`docid` int(11) NOT NULL,
`revnr` int(11) NOT NULL,
`revname` varchar(64) NOT NULL,
`signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`sign` int(11) NOT NULL,
`ritningsnr` varchar(128) NOT NULL,
`moment` varchar(256) NOT NULL,
`omrade` varchar(256) NOT NULL,
`start` datetime NOT NULL,
`stop` datetime NOT NULL,
`extTodo` int(11) NOT NULL,
PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='egenkontroll';
CREATE TABLE IF NOT EXISTS `table_2` (
`projid` int(11) NOT NULL,
`docid` int(11) NOT NULL,
`revnr` int(11) NOT NULL,
`revname` varchar(64) NOT NULL,
`signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`sign` int(11) NOT NULL,
`extWater` int(11) NOT NULL,
`extRisk` int(11) NOT NULL,
`extSystem` int(11) NOT NULL,
`extHelp` int(11) NOT NULL,
`extProvtryck` int(11) NOT NULL,
`extDoc` int(11) NOT NULL,
`extEgenkontroll` int(11) NOT NULL COMMENT 'exttabell',
`extOther` int(11) NOT NULL,
`extMontorer` int(11) NOT NULL,
PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='arbetsberedning';
CREATE TABLE IF NOT EXISTS `table_3` (
`projid` int(11) NOT NULL,
`docid` int(11) NOT NULL,
`revnr` int(11) NOT NULL,
`revname` varchar(64) NOT NULL,
`adress` varchar(256) NOT NULL,
`pipesMark` tinyint(1) NOT NULL,
`pipesKulvert` tinyint(1) NOT NULL,
`pipesBasement` tinyint(1) NOT NULL,
`pipesVaning` tinyint(1) NOT NULL,
`pipesIngjutna` tinyint(1) NOT NULL,
`ledningTappvatten` tinyint(1) NOT NULL,
`ledningVarmevatten` tinyint(1) NOT NULL,
`ledningHetvatten` tinyint(1) NOT NULL,
`ledningKylaPrim` tinyint(1) NOT NULL,
`ledningKylaSek` tinyint(1) NOT NULL,
`ledningGas` tinyint(1) NOT NULL,
`ledningLuft` tinyint(1) NOT NULL,
`ledningAvlopp` tinyint(1) NOT NULL,
`ledningOther` varchar(512) NOT NULL,
`materialGjutjarn` tinyint(1) NOT NULL,
`materialSteel` tinyint(1) NOT NULL,
`materialKoppar` tinyint(1) NOT NULL,
`materialPlast` tinyint(1) NOT NULL,
`materialRostfritt` tinyint(1) NOT NULL,
`materialOther` varchar(512) NOT NULL,
`omfattningLength` int(11) NOT NULL COMMENT 'meter',
`omfattningDimension` varchar(16) NOT NULL,
`omfattningRitningnr` varchar(128) NOT NULL,
`doneWithPump` tinyint(1) NOT NULL,
`doneWithVattenledning` tinyint(1) NOT NULL,
`doneWithKompressor` tinyint(1) NOT NULL,
`doneWithTathetsprovare` tinyint(1) NOT NULL,
`tryckmedieVatten` tinyint(1) NOT NULL,
`tryckmedieLuft` tinyint(1) NOT NULL,
`tryckmedieOther` varchar(128) NOT NULL,
`manometerDiameter` int(11) NOT NULL COMMENT 'mm',
`manometerGradering` int(11) NOT NULL COMMENT 'kPa',
`manometerReadPressure` int(11) NOT NULL,
`manometerTid` int(11) NOT NULL COMMENT 'sekunder',
`testedOn` datetime NOT NULL,
`testedBy` varchar(128) NOT NULL COMMENT '"id_" + (userid)',
`comments` varchar(1024) NOT NULL,
`commentsBy` varchar(128) NOT NULL COMMENT '"id_" + (userid)',
`signedOn` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`sign` int(11) NOT NULL,
PRIMARY KEY (`projid`,`docid`,`revnr`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
The fields I need are projid, docid, revnr, revname, signedOn, sign and are present in all current and future tables.