Mysql query failed after adding category table - mysql

When i run this query i'm keeping getting this error Database query failed 1054 - Unknown column 'cv.employeeIDFK' in 'on clause'
This only happen when I add the category in my query
FROM opjb_cv AS cv , opjb_cvCategory AS cv_cat
AND cv_cat.categoryIDFK IN ( 1,2,3,4,5,11,22,24,26,28 )
AND cv_cat.cvIDFK = cv.id
This is my query which is failing as you can see i have added all information but still its failing i cant seem to find anything wrong with this.
SELECT DISTINCT cv.id, cv.targetJobTitle, cv.targetJobTitleAlt, cv.recentEmployer, employee.firstName,
employee.surname, cv.recentJobTitle, cv.modifyAt, cv.city, cv.countryCountyFK, cv.countryStatesFK, cv.countryISO2FK, cv.experienceIDFK,
cv.careerIDFK, cv.areYouAuth, country.countryName, cv.employeeIDFK as user_id ,
match ( cv.title, cv.recentJobTitle, cv.targetJobTitle, cv.targetJobTitleAlt ) AGAINST ('desktop' IN BOOLEAN MODE) AS relevance
FROM opjb_cv AS cv , opjb_cvCategory AS cv_cat
JOIN opjb_employee AS employee ON cv.employeeIDFK = employee.id
JOIN opjb_country AS country ON cv.countryISO2FK=country.iso2
JOIN opjb_experience AS experience ON cv.experienceIDFK = experience.id
JOIN opjb_type AS type ON cv.jobTypeIDFK = type.id
JOIN opjb_education AS education ON cv.educationIDFK = education.id
JOIN opjb_countryStates as countryStates ON cv.countryStatesFK = countryStates.code
WHERE cv.showTo=1
AND cv.status=1
AND cv.countryISO2FK='GB'
AND match ( cv.title, cv.recentJobTitle, cv.targetJobTitle, cv.targetJobTitleAlt ) AGAINST ('desktop' IN BOOLEAN MODE )
AND cv_cat.categoryIDFK IN ( 1,2,3,4,5,11,22,24,26,28 )
AND cv_cat.cvIDFK = cv.id
AND experience.id=5
AND type.id=1
AND education.id=7
AND cv.modifyAt > NOW() - INTERVAL 3 DAY AND ( cv.salaryMin <= 48000 OR cv.salaryMax <= 48000 )
AND cv.countryStatesFK ='EG'
ORDER BY relevance DESC
These are all the tables which is involde in this query.
CREATE TABLE IF NOT EXISTS `opjb_country` (
`iso2` char(2) NOT NULL,
`iso3` char(3) NOT NULL,
`isoNo` smallint(3) NOT NULL,
`countryName` varchar(100) NOT NULL,
`regionIDFK` int(11) NOT NULL,
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`iso2`),
KEY `regionIDFK` (`regionIDFK`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_countryStates`
--
CREATE TABLE IF NOT EXISTS `opjb_countryStates` (
`code` varchar(40) NOT NULL default '',
`name` varchar(100) default NULL,
`countryISO2FK` char(2) NOT NULL default 'US',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`code`,`countryISO2FK`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_cv`
--
CREATE TABLE IF NOT EXISTS `opjb_cv` (
`id` int(11) NOT NULL auto_increment,
`type` tinyint(1) NOT NULL default '0',
`fileName` varchar(100) NOT NULL,
`fileType` varchar(15) NOT NULL,
`fileExe` varchar(5) NOT NULL,
`fileSize` int(11) NOT NULL default '0',
`filePath` varchar(255) NOT NULL,
`originalName` varchar(100) NOT NULL,
`fileHash` varchar(40) NOT NULL,
`title` varchar(30) NOT NULL,
`description` varchar(255) NOT NULL,
`showTo` tinyint(1) NOT NULL default '0',
`defaultCV` tinyint(1) NOT NULL default '0',
`targetJobTitle` varchar(100) NOT NULL,
`targetJobTitleAlt` varchar(100) NOT NULL,
`educationIDFK` int(6) NOT NULL default '0',
`careerIDFK` int(6) NOT NULL default '0',
`city` varchar(100) NOT NULL,
`areYouAuth` varchar(100) NOT NULL,
`recentJobTitle` varchar(100) NOT NULL,
`recentEmployer` varchar(100) NOT NULL,
`recentIndustry` varchar(100) NOT NULL,
`recentCareer` int(6) NOT NULL default '0',
`recentStartDate` date NOT NULL,
`recentEndDate` varchar(50) NOT NULL,
`jobTypeIDFK` int(6) NOT NULL default '0',
`jobStatusIDFK` int(6) NOT NULL default '0',
`salaryMin` varchar(20) NOT NULL default '0',
`salaryMax` varchar(20) NOT NULL default '0',
`salaryCurrency` varchar(5) NOT NULL default 'GBP',
`salaryType` tinyint(2) NOT NULL default '5',
`relocate` tinyint(1) NOT NULL default '0',
`willing_to_travel` tinyint(2) NOT NULL default '0',
`availability` tinyint(1) NOT NULL default '0',
`startDate` varchar(30) NOT NULL,
`positions` varchar(100) NOT NULL,
`userComments` text,
`noViews` int(7) NOT NULL default '0',
`status` tinyint(1) NOT NULL default '0',
`adminComments` text,
`employeeIDFK` int(11) NOT NULL default '0',
`countryISO2FK` char(2) NOT NULL default 'US',
`countryStatesFK` varchar(100) NOT NULL,
`countryCountyFK` varchar(100) NOT NULL,
`experienceIDFK` int(6) NOT NULL default '0',
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `employeeIDFK` (`employeeIDFK`),
KEY `countryISO2FK` (`countryISO2FK`),
FULLTEXT KEY `searchCV` (`title`,`targetJobTitle`,`targetJobTitleAlt`,`recentJobTitle`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_cvCategory`
--
CREATE TABLE IF NOT EXISTS `opjb_cvCategory` (
`cvIDFK` int(11) NOT NULL default '0',
`categoryIDFK` int(11) NOT NULL default '0',
PRIMARY KEY (`cvIDFK`,`categoryIDFK`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_education`
--
CREATE TABLE IF NOT EXISTS `opjb_education` (
`id` int(6) NOT NULL auto_increment,
`educationName` varchar(100) NOT NULL,
`lang` varchar(50) NOT NULL default 'english',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_employee`
--
CREATE TABLE IF NOT EXISTS `opjb_employee` (
`id` int(11) NOT NULL auto_increment,
`emailAddress` varchar(100) NOT NULL,
`username` varchar(30) NOT NULL,
`passwd` varchar(40) NOT NULL,
`title` varchar(20) NOT NULL,
`firstName` varchar(100) NOT NULL,
`middleName` varchar(50) NOT NULL,
`surname` varchar(100) NOT NULL,
`address` varchar(150) NOT NULL,
`address2` varchar(100) NOT NULL,
`city` varchar(100) NOT NULL,
`countryCountyFK` varchar(100) NOT NULL,
`countryStatesFK` varchar(100) NOT NULL,
`countryISO2FK` char(2) NOT NULL default 'US',
`postCode` varchar(20) NOT NULL,
`careerStatus` tinyint(1) NOT NULL default '0',
`contPref` tinyint(1) NOT NULL default '0',
`webSite` varchar(100) NOT NULL,
`job_title` varchar(255) NOT NULL,
`recent_employer` varchar(255) NOT NULL,
`mobile_no` varchar(30) NOT NULL,
`home_no` varchar(30) NOT NULL,
`categoryIDFK` int(6) default NULL,
`careerDegreeIDFK` int(6) default NULL,
`educationIDFK` int(6) default NULL,
`experienceIDFK` int(6) default NULL,
`pers_statement` text,
`actKey` varchar(100) NOT NULL,
`comments` varchar(255) NOT NULL,
`status` tinyint(1) NOT NULL default '0',
`isActive` tinyint(1) NOT NULL default '0',
`lastVisit` datetime NOT NULL,
`modifyAt` datetime NOT NULL,
`createdAt` datetime NOT NULL,
`createip` varchar(20) NOT NULL default '0',
`loginip` varchar(20) NOT NULL default '0',
PRIMARY KEY (`id`),
UNIQUE KEY `emailAddress` (`emailAddress`),
UNIQUE KEY `username` (`username`),
KEY `city` (`city`,`countryCountyFK`),
KEY `countryISO2FK` (`countryISO2FK`),
KEY `idx_fullname` (`firstName`,`surname`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_experience`
--
CREATE TABLE IF NOT EXISTS `opjb_experience` (
`id` int(6) NOT NULL auto_increment,
`experienceName` varchar(100) NOT NULL,
`lang` varchar(50) NOT NULL default 'english',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
-- --------------------------------------------------------
--
-- Table structure for table `opjb_type`
--
CREATE TABLE IF NOT EXISTS `opjb_type` (
`id` int(6) NOT NULL auto_increment,
`typeName` varchar(100) NOT NULL,
`lang` varchar(50) NOT NULL default 'english',
`isActive` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

Related

mysql 'slow queries log' return a huge number of rows examined but explain seems ok

The sql query
# Query_time: 16.536276 Lock_time: 0.000159 Rows_sent: 756 Rows_examined: 8392194
SET timestamp=1555422526;
SELECT c.id AS c__id, c.company_id AS c__company_id,
c.ordinary_price AS c__ordinary_price, c.nights AS c__nights,
c.food_type AS c__food_type, c.period AS c__period,
c.period_desc AS c__period_desc, c.extra AS c__extra,
c.coupons_bought AS c__coupons_bought, c.coupon_price AS c__coupon_price,
c.coordinates AS c__coordinates, c.best_price AS c__best_price,
c.from_price AS c__from_price, c.end_datetime AS c__end_datetime,
c.hide_clock AS c__hide_clock, c.hide_discount AS c__hide_discount,
c.booking_hotel_id AS c__booking_hotel_id, c.title AS c__title,
c.option_people AS c__option_people, c.option_room AS c__option_room,
c.option_period AS c__option_period, c.city AS c__city,
c2.id AS c2__id, c2.people AS c2__people, c2.room AS c2__room,
( SELECT c8.url AS c8__url
FROM campaign_images c8
WHERE (c8.campaign_id = c.id
AND c8.photo_type = 'list')
ORDER BY c8.ordering ASC LIMIT 1
) AS c__0
FROM campaign c
LEFT JOIN campaign_options c2 ON c.id = c2.campaign_id
AND (c2.active = 1)
LEFT JOIN city_in_campaign c3 ON c.id = c3.campaign_id
LEFT JOIN city c4 ON c3.city_id = c4.id
LEFT JOIN company c5 ON c.company_id = c5.id
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
LEFT JOIN campaign_in_group c7 ON c.id = c7.campaign_id
WHERE c.id IN ('13308', '13281', '13265')
AND (c.status IN ('published')
AND c.start_datetime <= NOW()
AND c.end_datetime >= NOW()
AND c5.id = '2111'
AND c.id != '14624'
AND (c7.group_id in (1)
OR c7.group_id is NULL
)
)
ORDER BY c.coupon_expire_datetime ASC;
create table campaign
CREATE TABLE `campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`company_id` int(11) DEFAULT NULL,
`source_id` int(11) DEFAULT NULL,
`secondary_source_id` int(11) DEFAULT NULL,
`source_hotel_id` int(11) DEFAULT NULL,
`booking_hotel_id` int(11) DEFAULT NULL,
`booking_board_id` int(11) DEFAULT NULL,
`booking_rate` varchar(100) DEFAULT NULL,
`ordinary_price` decimal(9,2) NOT NULL,
`best_price` tinyint(1) NOT NULL DEFAULT '0',
`from_price` tinyint(1) NOT NULL DEFAULT '0',
`status` varchar(10) DEFAULT 'draft',
`type` varchar(20) DEFAULT 'other',
`deal_status` varchar(20) DEFAULT 'in_progress',
`coupon_price` decimal(9,2) NOT NULL,
`cosmote_discount` decimal(5,2) DEFAULT NULL,
`nights` int(11) NOT NULL DEFAULT '0',
`period` varchar(255) DEFAULT NULL,
`period_desc` varchar(1000) DEFAULT NULL,
`hide_period` tinyint(1) NOT NULL DEFAULT '0',
`food_type` varchar(100) DEFAULT NULL,
`stars` varchar(100) DEFAULT NULL,
`adults` tinyint(1) DEFAULT NULL,
`childs` tinyint(1) DEFAULT NULL,
`extra` varchar(255) DEFAULT NULL,
`best_point` varchar(511) DEFAULT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_desc` varchar(500) DEFAULT NULL,
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`family_packages_desc` varchar(1000) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`start_datetime` datetime DEFAULT NULL,
`end_datetime` datetime DEFAULT NULL,
`coupon_expire_datetime` datetime DEFAULT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
`city` varchar(255) DEFAULT NULL,
`min_coupons` int(11) NOT NULL DEFAULT '0',
`is_global` tinyint(4) NOT NULL DEFAULT '0',
`hide_clock` tinyint(1) NOT NULL DEFAULT '0',
`hide_discount` tinyint(1) NOT NULL DEFAULT '0',
`hide_purchases` tinyint(1) NOT NULL DEFAULT '0',
`booking_enabled` tinyint(1) NOT NULL DEFAULT '0',
`booking_phone` varchar(50) DEFAULT NULL,
`refresh` tinyint(1) NOT NULL DEFAULT '0',
`installments` tinyint(1) NOT NULL DEFAULT '1',
`receipt` enum('0','1') NOT NULL DEFAULT '0',
`newsletters_sent` tinyint(4) NOT NULL DEFAULT '0',
`max_coupons` int(11) NOT NULL DEFAULT '0',
`max_coupons_per_user` int(11) DEFAULT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`fake_orders` int(11) DEFAULT '0',
`title` varchar(255) DEFAULT NULL,
`newsletter_title` varchar(500) DEFAULT NULL,
`linkwise_title` varchar(500) DEFAULT NULL,
`option_title` varchar(255) DEFAULT NULL,
`option_title_en` varchar(255) DEFAULT NULL,
`option_people` varchar(150) DEFAULT NULL,
`option_room` varchar(150) DEFAULT NULL,
`option_period` varchar(150) DEFAULT NULL,
`name` varchar(1200) DEFAULT NULL,
`description` text,
`highlights` text,
`coupon_instructions` text,
`show_in_recent_deals` tinyint(4) NOT NULL DEFAULT '1',
`youtube_video_id` varchar(100) DEFAULT NULL,
`in_side` tinyint(4) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`send_newsletter` tinyint(4) NOT NULL DEFAULT '1',
`resend_newsletter` tinyint(4) NOT NULL DEFAULT '0',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`ordering` int(11) unsigned DEFAULT '0',
PRIMARY KEY (`id`),
KEY `company_id_idx` (`company_id`),
KEY `user_id_idx` (`user_id`),
KEY `status_indx` (`status`),
KEY `str_dt_indx` (`start_datetime`),
KEY `end_dt_indx` (`end_datetime`),
KEY `side_indx` (`in_side`),
KEY `ord_indx` (`ordering`),
KEY `global_indx` (`is_global`),
KEY `act_indx` (`active`),
KEY `coup_expr_index` (`coupon_expire_datetime`)
) ENGINE=InnoDB AUTO_INCREMENT=14788 DEFAULT CHARSET=utf8
create table campaign_options
CREATE TABLE `campaign_options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`coupons_bought` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
`name_en` varchar(255) DEFAULT NULL,
`people` varchar(100) DEFAULT NULL,
`room` varchar(100) DEFAULT NULL,
`food` varchar(100) DEFAULT NULL,
`period` varchar(100) DEFAULT NULL,
`coupon_price` decimal(9,2) NOT NULL,
`extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`high_season_price` decimal(9,2) DEFAULT '0.00',
`high_season_extra_night_price` decimal(9,2) NOT NULL DEFAULT '0.00',
`modified_datetime` timestamp NULL DEFAULT NULL,
`created_datetime` datetime NOT NULL,
`main` tinyint(1) NOT NULL DEFAULT '0',
`family` tinyint(1) NOT NULL DEFAULT '0',
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `active_indx` (`active`),
KEY `family_indx` (`family`),
KEY `main_indx` (`main`)
) ENGINE=InnoDB AUTO_INCREMENT=48990 DEFAULT CHARSET=utf8
create table city_in_campaign
CREATE TABLE `city_in_campaign` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `city_id_idx` (`city_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=227176 DEFAULT CHARSET=utf8
create table city
CREATE TABLE `city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`is_active` tinyint(3) unsigned DEFAULT NULL,
`modified_datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `country_id_idx` (`country_id`),
KEY `is_active_indx` (`is_active`)
) ENGINE=InnoDB AUTO_INCREMENT=254 DEFAULT CHARSET=utf8
create table company
CREATE TABLE `company` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`ref_id` int(11) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`description` text,
`logo` varchar(255) DEFAULT NULL,
`phone_number` varchar(100) DEFAULT NULL,
`address` varchar(200) DEFAULT NULL,
`coordinates` varchar(70) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`website` varchar(255) DEFAULT NULL,
`skype_name` varchar(50) DEFAULT NULL,
`icq_number` varchar(255) DEFAULT NULL,
`payment_information` text,
`extra1` text,
`extra2` text,
`extra3` text,
`video` varchar(500) DEFAULT NULL,
`checked` tinyint(1) NOT NULL DEFAULT '0',
`ordering` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `checked_indx` (`checked`),
KEY `ordering_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=2519 DEFAULT CHARSET=utf8
create table campaign_in_category
CREATE TABLE `campaign_in_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) NOT NULL,
`category_id` int(11) DEFAULT NULL,
`ordering` int(11) NOT NULL DEFAULT '0',
`created_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `campaign_id_idx` (`campaign_id`),
KEY `category_id_idx` (`category_id`),
KEY `order_indx` (`ordering`)
) ENGINE=InnoDB AUTO_INCREMENT=457080 DEFAULT CHARSET=utf8
create table campaign_in_group
CREATE TABLE `campaign_in_group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`campaign_id` int(11) DEFAULT NULL,
`group_id` int(11) DEFAULT NULL,
`created_datetime` datetime DEFAULT NULL,
`modified_datetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `camp_group_indx` (`campaign_id`,`group_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1175 DEFAULT CHARSET=utf8
campaign total rows: 12,657
campaign_options total rows: 43,714
city_in_campaign total rows: 15,162
city total rows: 215
company total rows: 1,756
campaign_in_category total rows: 38,817
campaign_in_group total rows: 395
explain of the query
which ones have index
it looks you need composite index because file sorting.
usage:
CREATE INDEX index_name
ON table_name(c2,c3,c4);
You are getting above result because some time o/p of explain gives different result as response of query of prod.
For optimization of query you can't have a fix answer. it varies from case to case. As for this situation I think you need to perform performance testing of this query. You can do this by inserting some records in all referenced table and then checking it's performance using explain.
LEFT JOIN campaign_in_category c6 ON c.id = c6.campaign_id
seems to be totally useless. But the Optimizer may not realize it. Remove it and any other dead code.

osticket how to get ticket id by the form entry value?

In osticket we have a number of custom form fields setup and I need help querying them. The data that I want to query is stored in form_entry.value.
SELECT * FROM `form_entry_values` fev, `form_entry` fe WHERE fev.value = '{$ibn}' AND fe.id = fev.entry_id
This is returning the correct data but how can I find the ticket ID if i know the form_id? I can't seem to find any joining tables.
This is the full db: https://github.com/osTicket/osTicket-1.8/blob/b1c845bf0591b1f5da593a55e462b07e5a4ee5de/setup/inc/streams/core/install-mysql.sql
DROP TABLE IF EXISTS `form`;
CREATE TABLE `form` (
`id` int(11) unsigned NOT NULL auto_increment,
`type` varchar(8) NOT NULL DEFAULT 'G',
`deletable` tinyint(1) NOT NULL DEFAULT 1,
`title` varchar(255) NOT NULL,
`instructions` varchar(512),
`notes` text,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_field`;
CREATE TABLE `form_field` (
`id` int(11) unsigned NOT NULL auto_increment,
`form_id` int(11) unsigned NOT NULL,
`type` varchar(255) NOT NULL DEFAULT 'text',
`label` varchar(255) NOT NULL,
`required` tinyint(1) NOT NULL DEFAULT 0,
`private` tinyint(1) NOT NULL DEFAULT 0,
`edit_mask` tinyint(1) NOT NULL DEFAULT 0,
`name` varchar(64) NOT NULL,
`configuration` text,
`sort` int(11) unsigned NOT NULL,
`hint` varchar(512),
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_entry`;
CREATE TABLE `form_entry` (
`id` int(11) unsigned NOT NULL auto_increment,
`form_id` int(11) unsigned NOT NULL,
`object_id` int(11) unsigned,
`object_type` char(1) NOT NULL DEFAULT 'T',
`sort` int(11) unsigned NOT NULL DEFAULT 1,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `entry_lookup` (`object_type`, `object_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `form_entry_values`;
CREATE TABLE `form_entry_values` (
-- references form_entry.id
`entry_id` int(11) unsigned NOT NULL,
`field_id` int(11) unsigned NOT NULL,
`value` text,
`value_id` int(11),
PRIMARY KEY (`entry_id`, `field_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `list`;
CREATE TABLE `list` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`name_plural` varchar(255),
`sort_mode` enum('Alpha', '-Alpha', 'SortCol') NOT NULL DEFAULT 'Alpha',
`masks` int(11) unsigned NOT NULL DEFAULT 0,
`type` VARCHAR( 16 ) NULL DEFAULT NULL,
`notes` text,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `list_items`;
CREATE TABLE `list_items` (
`id` int(11) unsigned NOT NULL auto_increment,
`list_id` int(11),
`status` int(11) unsigned NOT NULL DEFAULT 1,
`value` varchar(255) NOT NULL,
-- extra value such as abbreviation
`extra` varchar(255),
`sort` int(11) NOT NULL DEFAULT 1,
`properties` text,
PRIMARY KEY (`id`),
KEY `list_item_lookup` (`list_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket`;
CREATE TABLE `ticket` (
`ticket_id` int(11) unsigned NOT NULL auto_increment,
`number` varchar(20),
`user_id` int(11) unsigned NOT NULL default '0',
`user_email_id` int(11) unsigned NOT NULL default '0',
`status_id` int(10) unsigned NOT NULL default '0',
`dept_id` int(10) unsigned NOT NULL default '0',
`sla_id` int(10) unsigned NOT NULL default '0',
`topic_id` int(10) unsigned NOT NULL default '0',
`staff_id` int(10) unsigned NOT NULL default '0',
`team_id` int(10) unsigned NOT NULL default '0',
`email_id` int(11) unsigned NOT NULL default '0',
`flags` int(10) unsigned NOT NULL default '0',
`ip_address` varchar(64) NOT NULL default '',
`source` enum('Web','Email','Phone','API','Other') NOT NULL default 'Other',
`isoverdue` tinyint(1) unsigned NOT NULL default '0',
`isanswered` tinyint(1) unsigned NOT NULL default '0',
`duedate` datetime default NULL,
`reopened` datetime default NULL,
`closed` datetime default NULL,
`lastmessage` datetime default NULL,
`lastresponse` datetime default NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`ticket_id`),
KEY `user_id` (`user_id`),
KEY `dept_id` (`dept_id`),
KEY `staff_id` (`staff_id`),
KEY `team_id` (`team_id`),
KEY `status_id` (`status_id`),
KEY `created` (`created`),
KEY `closed` (`closed`),
KEY `duedate` (`duedate`),
KEY `topic_id` (`topic_id`),
KEY `sla_id` (`sla_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_attachment`;
CREATE TABLE `ticket_attachment` (
`attach_id` int(11) unsigned NOT NULL auto_increment,
`ticket_id` int(11) unsigned NOT NULL default '0',
`file_id` int(10) unsigned NOT NULL default '0',
`ref_id` int(11) unsigned NOT NULL default '0',
`inline` tinyint(1) NOT NULL default '0',
`created` datetime NOT NULL,
PRIMARY KEY (`attach_id`),
KEY `ticket_id` (`ticket_id`),
KEY `ref_id` (`ref_id`),
KEY `file_id` (`file_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_lock`;
CREATE TABLE `ticket_lock` (
`lock_id` int(11) unsigned NOT NULL auto_increment,
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(10) unsigned NOT NULL default '0',
`expire` datetime default NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`lock_id`),
UNIQUE KEY `ticket_id` (`ticket_id`),
KEY `staff_id` (`staff_id`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_email_info`;
CREATE TABLE `ticket_email_info` (
`id` int(11) unsigned NOT NULL auto_increment,
`thread_id` int(11) unsigned NOT NULL,
`email_mid` varchar(255) NOT NULL,
`headers` text,
PRIMARY KEY (`id`),
KEY `email_mid` (`email_mid`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_event`;
CREATE TABLE `ticket_event` (
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(11) unsigned NOT NULL,
`team_id` int(11) unsigned NOT NULL,
`dept_id` int(11) unsigned NOT NULL,
`topic_id` int(11) unsigned NOT NULL,
`state` enum('created','closed','reopened','assigned','transferred','overdue') NOT NULL,
`staff` varchar(255) NOT NULL default 'SYSTEM',
`annulled` tinyint(1) unsigned NOT NULL default '0',
`timestamp` datetime NOT NULL,
KEY `ticket_state` (`ticket_id`, `state`, `timestamp`),
KEY `ticket_stats` (`timestamp`, `state`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_status`;
CREATE TABLE IF NOT EXISTS `ticket_status` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(60) NOT NULL DEFAULT '',
`state` varchar(16) DEFAULT NULL,
`mode` int(11) unsigned NOT NULL DEFAULT '0',
`flags` int(11) unsigned NOT NULL DEFAULT '0',
`sort` int(11) unsigned NOT NULL DEFAULT '0',
`properties` text NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `state` (`state`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_priority`;
CREATE TABLE `ticket_priority` (
`priority_id` tinyint(4) NOT NULL auto_increment,
`priority` varchar(60) NOT NULL default '',
`priority_desc` varchar(30) NOT NULL default '',
`priority_color` varchar(7) NOT NULL default '',
`priority_urgency` tinyint(1) unsigned NOT NULL default '0',
`ispublic` tinyint(1) NOT NULL default '1',
PRIMARY KEY (`priority_id`),
UNIQUE KEY `priority` (`priority`),
KEY `priority_urgency` (`priority_urgency`),
KEY `ispublic` (`ispublic`)
) DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `ticket_thread`;
CREATE TABLE `ticket_thread` (
`id` int(11) unsigned NOT NULL auto_increment,
`pid` int(11) unsigned NOT NULL default '0',
`ticket_id` int(11) unsigned NOT NULL default '0',
`staff_id` int(11) unsigned NOT NULL default '0',
`user_id` int(11) unsigned not null default 0,
`thread_type` enum('M','R','N') NOT NULL,
`poster` varchar(128) NOT NULL default '',
`source` varchar(32) NOT NULL default '',
`title` varchar(255),
`body` mediumtext NOT NULL,
`format` varchar(16) NOT NULL default 'html',
`ip_address` varchar(64) NOT NULL default '',
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `ticket_id` (`ticket_id`),
KEY `staff_id` (`staff_id`),
KEY `pid` (`pid`)
) DEFAULT CHARSET=utf8;
CREATE TABLE `ticket_collaborator` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`isactive` tinyint(1) NOT NULL DEFAULT '1',
`ticket_id` int(11) unsigned NOT NULL DEFAULT '0',
`user_id` int(11) unsigned NOT NULL DEFAULT '0',
-- M => (message) clients, N => (note) 3rd-Party, R => (reply) external authority
`role` char(1) NOT NULL DEFAULT 'M',
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `collab` (`ticket_id`,`user_id`)
) DEFAULT CHARSET=utf8;
In the values table, there is a field object_id this is the ticket ID.
SELECT t.* FROM form_entry_values fev
LEFT JOIN form_entry fe ON(fev.entry_id = fe.id)
LEFT JOIN ticket t ON(fe.object_id = t.ticket_id)
WHERE fev.value = '123'
in the Osticket 1.15 version, the object_id in the form_entry has a relation to the user_id.
SELECT t.* FROM form_entry_values fev
LEFT JOIN form_entry fe ON fev.entry_id = fe.id
LEFT JOIN ticket t ON fe.object_id = t.user_id

Indexes in Mysql table

I have such tables:
CREATE TABLE `skadate_newsfeed_action` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`entityId` int(11) NOT NULL,
`entityType` varchar(100) NOT NULL,
`feature` varchar(100) NOT NULL,
`data` longtext NOT NULL,
`status` varchar(20) NOT NULL DEFAULT 'active',
`createTime` int(11) NOT NULL,
`updateTime` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`visibility` int(11) NOT NULL,
`privacy` enum('everybody','friends_only') NOT NULL DEFAULT 'everybody',
PRIMARY KEY (`id`),
KEY `userId` (`userId`),
KEY `privacy` (`visibility`),
KEY `updateTime` (`updateTime`),
KEY `entity` (`entityType`,`entityId`)
) ENGINE=MyISAM;
CREATE TABLE `skadate_profile` (
`profile_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(32) NOT NULL DEFAULT '',
`password` varchar(40) NOT NULL,
`sex` bigint(20) DEFAULT NULL,
`match_sex` bigint(20) DEFAULT NULL,
`birthdate` date NOT NULL DEFAULT '0000-00-00',
`headline` varchar(128) DEFAULT '',
`general_description` text,
`match_agerange` varchar(6) DEFAULT NULL,
`custom_location` varchar(255) DEFAULT NULL,
`country_id` char(2) NOT NULL DEFAULT '',
`zip` varchar(10) DEFAULT NULL,
`state_id` varchar(5) DEFAULT NULL,
`city_id` int(11) DEFAULT '0',
`join_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`activity_stamp` int(10) unsigned NOT NULL DEFAULT '0',
`membership_type_id` int(10) unsigned NOT NULL DEFAULT '18',
`affiliate_id` int(8) unsigned NOT NULL DEFAULT '0',
`email_verified` enum('undefined','yes','no') NOT NULL DEFAULT 'undefined',
`reviewed` enum('n','y') NOT NULL DEFAULT 'n',
`has_photo` enum('n','y') NOT NULL DEFAULT 'n',
`has_media` enum('n','y') NOT NULL DEFAULT 'n',
`status` enum('active','on_hold','suspended') NOT NULL DEFAULT 'active',
`featured` enum('n','y') NOT NULL DEFAULT 'n',
`register_invite_score` tinyint(3) NOT NULL DEFAULT '0',
`rate_score` tinyint(3) unsigned NOT NULL DEFAULT '0',
`rates` bigint(20) unsigned NOT NULL DEFAULT '0',
`language_id` int(10) unsigned NOT NULL DEFAULT '0',
`join_ip` int(11) unsigned NOT NULL DEFAULT '0',
`neigh_location` enum('country','state','city','zip') DEFAULT NULL,
`neigh_location_distance` int(10) unsigned NOT NULL DEFAULT '0',
`bg_color` varchar(32) DEFAULT NULL,
`bg_image` varchar(32) DEFAULT NULL,
`bg_image_url` varchar(255) DEFAULT NULL,
`bg_image_mode` tinyint(1) DEFAULT NULL,
`bg_image_status` enum('active','approval') NOT NULL DEFAULT 'active',
`has_music` enum('n','y') NOT NULL DEFAULT 'n',
`is_private` tinyint(1) NOT NULL DEFAULT '0',
`subscription_id_offerit` text,
PRIMARY KEY (`profile_id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`),
KEY `membership_id` (`membership_type_id`),
KEY `zip` (`zip`),
KEY `country_id` (`country_id`),
KEY `state_id` (`state_id`),
KEY `city_id` (`city_id`),
KEY `sex` (`sex`),
KEY `match_sex` (`match_sex`),
KEY `activity_stamp` (`activity_stamp`),
KEY `join_stamp` (`join_stamp`),
KEY `birthdate` (`birthdate`),
KEY `featured` (`featured`,`has_photo`,`activity_stamp`)
) ENGINE=MyISAM;
And try to perform this query:
SELECT DISTINCT `na`.*
FROM `skadate_newsfeed_action` AS `na`
LEFT JOIN `skadate_profile` AS `profile` ON ( `na`.`userId` = `profile`.`profile_id` )
WHERE ( profile.email_verified='yes' OR profile.email_verified='no' OR profile.email_verified='undefined' )
AND `profile`.`status`='active' AND `na`.`status`='active' AND `na`.`privacy`='everybody'
AND `na`.`visibility` & 1 AND `na`.`updateTime` < 1455885224
ORDER BY `na`.`updateTime` DESC, `na`.`id` DESC
LIMIT 0, 10
But when I see EXPLAIN:
Maybe someone can help me, how I can improve this query?
If you want records from only one table, then use exists rather than a join and select distinct. So:
SELECT na.*
FROM `skadate_newsfeed_action` na
WHERE EXISTS (SELECT 1
FROM skadate_profile p
WHERE na.userId = p.profile_id AND
p.email_verified IN ('yes', 'no', 'undefined') AND
p.status = 'active'
) AND
na.status = 'active' AND
na.privacy = 'everybody' AND
na.visibility & 1 > 0 AND
na.updateTime < 1455885224
ORDER BY na.`updateTime` DESC, na.`id` DESC
LIMIT 0, 10;
For this query, you want an index on skadate_profile(profile_id, status, verified). Also, the following index is probably helpful: skadate_newsfeed_action(status, privacy, updateTime, visibility, userId).
This is probably because of the DISTICT keyword. To remove duplicates MySQL needs to sort the result by every selected column.

#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

#1415 - Not allowed to return a result set from a trigger

DELIMITER $$
CREATE TRIGGER `msg_after_insert`
AFTER INSERT ON `meter_reading`
FOR EACH ROW
BEGIN
select meter_reading.*,a.amount as Light_Reading_Amt,b.amount as Power_Reading_Amt from meter_reading inner join master_unit a on a.min_unit<=msg1 and msg1<=a.max_unit and doc>=a.date_of_enter_value_fm inner join master_unit b on b.min_unit<=msg2 and msg2<=b.max_unit and doc<=b.date_of_enter_value_to;
INSERT INTO `smsd`.`outbox` (DestinationNumber,TextDecoded) VALUES (NEW.mobno, CONCAT_WS(NEW.msg1,NEW.Light_Reading_Amt,' ',NEW.msg2,NEW.Light_Reading_Amt));
END $$
DELIMITER ;
My databases are
1. mr_sms (tables are : 1.meter_reading 2.master_unit)
--meter_reading table--
CREATE TABLE IF NOT EXISTS `meter_reading` (
`serno` int(50) NOT NULL AUTO_INCREMENT,
`cno` varchar(50) DEFAULT NULL,
`accn_no` varchar(20) DEFAULT NULL,
`loc` varchar(20) DEFAULT NULL,
`type_of_accn` varchar(50) NOT NULL,
`rank` varchar(25) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`mobno` varchar(15) DEFAULT NULL,
`msg1` int(100) DEFAULT NULL,
`msg2` int(250) NOT NULL,
`status` varchar(30) NOT NULL,
`doc` date NOT NULL,
`remarks` varchar(100) DEFAULT NULL,
PRIMARY KEY (`serno`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
--master_unit table--
CREATE TABLE IF NOT EXISTS `master_unit` (
`u_id` int(11) NOT NULL AUTO_INCREMENT,
`min_unit` int(150) NOT NULL,
`max_unit` int(200) NOT NULL,
`rate` varchar(20) NOT NULL,
`amount` varchar(20) NOT NULL,
`duty` varchar(20) NOT NULL,
`dutyamount` varchar(20) NOT NULL,
`date_of_enter_value_fm` date NOT NULL,
`date_of_enter_value_to` date NOT NULL,
PRIMARY KEY (`u_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
2. smsd (table is : outbox)
--
-- Table structure for table `outbox`
--
CREATE TABLE IF NOT EXISTS `outbox` (
`UpdatedInDB` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`InsertIntoDB` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendingDateTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`SendBefore` time NOT NULL DEFAULT '23:59:59',
`SendAfter` time NOT NULL DEFAULT '00:00:00',
`Text` text,
`DestinationNumber` varchar(20) NOT NULL DEFAULT '',
`Coding` enum('Default_No_Compression','Unicode_No_Compression','8bit','Default_Compression','Unicode_Compression') NOT NULL DEFAULT 'Default_No_Compression',
`UDH` text,
`Class` int(11) DEFAULT '-1',
`TextDecoded` text NOT NULL,
`ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`MultiPart` enum('false','true') DEFAULT 'false',
`RelativeValidity` int(11) DEFAULT '-1',
`SenderID` varchar(255) DEFAULT NULL,
`SendingTimeOut` timestamp NULL DEFAULT '0000-00-00 00:00:00',
`DeliveryReport` enum('default','yes','no') DEFAULT 'default',
`CreatorID` text NOT NULL,
PRIMARY KEY (`ID`),
KEY `outbox_date` (`SendingDateTime`,`SendingTimeOut`),
KEY `outbox_sender` (`SenderID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `outbox`
--