I have an Users Table with a staff column and a remove column.
I don't want to show Users that have the remove column with the value of 1
SELECT *
FROM Users
WHERE
Users.staff = 1
AND Users.remove != 1
I don't get an error but my code doesn't work.
this is the schema
CREATE TABLE `Users` (
`userId` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fullName` varchar(50) DEFAULT NULL,
`firstName` varchar(25) NOT NULL DEFAULT '',
`lastName` varchar(25) NOT NULL DEFAULT '',
`address` varchar(50) NOT NULL DEFAULT '',
`city` varchar(25) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
`zipCode` varchar(25) DEFAULT NULL,
`email` varchar(50) NOT NULL DEFAULT '',
`cellPhone` varchar(15) DEFAULT NULL,
`birthDate` date NOT NULL,
`creditCard` varchar(250) NOT NULL DEFAULT '',
`subscriptionStarted` date NOT NULL,
`subscriptionEnded` date NOT NULL,
`basicPlan` tinyint(1) DEFAULT NULL,
`standardPlan` tinyint(1) DEFAULT NULL,
`premiumPlan` tinyint(1) DEFAULT NULL,
`remove` tinyint(1) DEFAULT NULL,
`staff` tinyint(1) DEFAULT NULL,
`admin` tinyint(1) DEFAULT NULL,
`systemAdmin` tinyint(1) DEFAULT NULL,
`edited` datetime DEFAULT NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;
both work for me Boss.
create table users
( staff int not null,
remove int not null
);
insert users (staff,remove) values (1,0);
insert users (staff,remove) values (1,1);
insert users (staff,remove) values (2,0);
insert users (staff,remove) values (2,1);
SELECT *
FROM Users
WHERE
Users.staff = 1
AND Users.remove != 1
SELECT *
FROM Users
WHERE
Users.staff = 1
AND Users.remove <> 1
Edit due to nullability:
SELECT * FROM Users
WHERE staff=1
and remove <> 1 or remove is null
SELECT * FROM Users
WHERE staff=1
and remove is not null
Related
I have 5 tables named: schools, candidates, candidate_subjects, subjects, lgas
Each school belong to a lga, each candidate belong to a school, each candidate registers subjects
Below are the table structures:
CREATE TABLE `subjects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT NULL,
`code` varchar(10) DEFAULT NULL,
`exam_type_id` int(11) DEFAULT NULL,
`status` int(11) DEFAULT NULL,
`type` varchar(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=60 DEFAULT CHARSET=latin1;
CREATE TABLE `candidates` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`exam_no` varchar(20) DEFAULT NULL,
`surname` varchar(100) DEFAULT NULL,
`other_names` varchar(150) DEFAULT NULL,
`school_id` int(11) DEFAULT NULL,
`exam_type_id` int(11) DEFAULT NULL,
`dob` varchar(12) DEFAULT NULL,
`sex` varchar(6) DEFAULT NULL,
`no_of_subjects` int(2) DEFAULT NULL,
`nationality` varchar(20) DEFAULT NULL,
`state` varchar(20) DEFAULT NULL,
`lga` varchar(20) DEFAULT NULL,
`exam_year` varchar(4) DEFAULT NULL,
`date_created` varchar(255) DEFAULT NULL,
`date_modified` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`registration_completed` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=28034 DEFAULT CHARSET=latin1;
CREATE TABLE `candidate_subjects` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`candidate_id` int(11) DEFAULT NULL,
`exam_type_id` int(10) DEFAULT NULL,
`subject_id` int(10) DEFAULT NULL,
`ca_score` int(11) DEFAULT NULL,
`exam_score` int(6) DEFAULT NULL,
`score_grade` varchar(10) DEFAULT NULL,
`date_created` varchar(10) DEFAULT NULL,
`date_modified` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=327740 DEFAULT CHARSET=latin1;
CREATE TABLE `schools` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`passcode` varchar(15) DEFAULT NULL,
`code` varchar(10) DEFAULT NULL,
`name` varchar(200) DEFAULT NULL,
`lga` int(11) DEFAULT NULL,
`address` varchar(250) DEFAULT NULL,
`phone` varchar(50) DEFAULT NULL,
`exam_year` varchar(10) DEFAULT NULL,
`eo_name` varchar(100) DEFAULT NULL,
`eo_phone` varchar(50) DEFAULT NULL,
`eo_email` varchar(100) DEFAULT NULL,
`date_created` varchar(10) DEFAULT NULL,
`date_modified` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`profile_created` int(1) DEFAULT NULL,
`entries_purchased` int(11) DEFAULT NULL,
`entries_used` int(11) DEFAULT NULL,
`entries_remaining` int(11) DEFAULT NULL,
`scratchcard_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=652 DEFAULT CHARSET=latin1;
CREATE TABLE `lgas` (
`lga_id` int(11) NOT NULL AUTO_INCREMENT,
`lga_name` varchar(250) NOT NULL,
`state_id` varchar(20) NOT NULL,
PRIMARY KEY (`lga_id`)
) ENGINE=InnoDB AUTO_INCREMENT=786 DEFAULT CHARSET=latin1;
I want to generate a report for each lga like this:
S/No| Name of School |Eng|Mth|B.sc|....Total registered
That is name of school from the selected lga, total number of students from that school that registered for Eng, Mth, B.sc ... Total number of students that registered those subjects from that school
SELECT
b.name as school_name,
sum(case when e.name = 'Eng' then 1 else 0 end) as english_students,
sum(case when e.name = 'Mth' then 1 else 0 end) as math_students,
sum(case when e.name = 'B.sc' then 1 else 0 end) as whatever_this_is_students
FROM lgas a
LEFT JOIN schools b ON a.lga_id = b.lga
LEFT JOIN candidates c on b.id = c.school_id
LEFT JOIN candidate_subjects d on c.id = d.candidate_id
LEFT JOIN subjects e on d.subject_id = e.id
WHERE a.lda_id = 'selected_id'
GROUP by school_name;
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`
--
I have such sql query
SELECT *,(p.datetime-u.createtime)/86400 as result
FROM tbl_user_points p
Inner join tbl_users u ON u.id=p.user_id
GROUP BY p.user_id
HAVING SUM(p.points) > 100
order by SUM(p.points)
i need to find the datetime( the row in table tbl_user_points), when each users reached.
For more information Here the schemata of table
tbl_user_points
CREATE TABLE `tbl_user_points` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`action` int(10) unsigned NOT NULL,
`description` varchar(255) DEFAULT '',
`points` int(10) NOT NULL DEFAULT '0',
`datetime` int(10) unsigned NOT NULL,
`club_id` int(10) unsigned DEFAULT NULL,
`event_id` int(10) unsigned DEFAULT NULL,
`location_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='Achieved users points for any actions.';
Here the schemata of table
tbl_users
CREATE TABLE `tbl_users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`email` varchar(128) NOT NULL,
`activkey` varchar(128) NOT NULL DEFAULT '',
`createtime` int(11) NOT NULL,
`lastvisit` int(11) NOT NULL,
`superuser` int(1) NOT NULL DEFAULT '0',
`status` int(1) NOT NULL DEFAULT '1',
`first_name` varchar(128) DEFAULT NULL,
`last_name` varchar(128) DEFAULT NULL,
`gender` varchar(6) DEFAULT NULL,
`locale` varchar(45) DEFAULT NULL,
`service` varchar(45) NOT NULL,
`service_id` varchar(255) DEFAULT NULL,
`location` varchar(128) DEFAULT NULL,
`state` int(3) DEFAULT NULL,
`photo` varchar(255) DEFAULT NULL,
`city` varchar(125) DEFAULT NULL,
`about_me` varchar(255) DEFAULT NULL,
`user_code` varchar(10) DEFAULT NULL,
`loyalty_level` varchar(45) DEFAULT 'basic' COMMENT 'Level of loyalty for current user',
`updatetime` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `user_code_UNIQUE` (`user_code`),
KEY `status` (`status`),
KEY `superuser` (`superuser`)
) ENGINE=InnoDB AUTO_INCREMENT=1033 DEFAULT CHARSET=utf8;
sample input
tbl_user_points
id | user_id|action|description|points| datetime
2246 1 1 First visit 5 1383331212
2254 1 2 Second visit 15 1383354853
2255 1 3 Winner 25 1383360231
2256 2 1 First visit 5 1383331202
2257 2 2 Second visit 15 1383354553
2258 2 3 Winner 25 1383360211
tbl_user simple,for example
id=1,createtime=1313000000
id=2,createtime=1313000001
for HAVING SUM(p.points) > 15
output should be
user_id datetime
1 1383354853
2 1383354553
Use CASE syntax. Check this link:
http://dev.mysql.com/doc/refman/5.0/en/case.html.
You can achieve it by using statements like this:
Select X, case when y>100 then desired_row when y<100 then desired_row end as 'desired_row' from your_table where column=your_condition order by 1
Here you're conditioning the select clause by as many cases or conditions you requiere.
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 ;
What I'm attempting to accomplish is return a SELECT statement of ALL duplicates in a table within given filters.
I'm attempting to run the following query but every time I run it my server locks up and it never completes the query. I have no idea what is causing this and some help would be greatly appreciated on either how to fix this or to accomplish my goal in another manner.
EDIT: I've added the EXPLAIN data as requested!
EDIT 2: I've added the CREATE statements as requested!
SELECT *
FROM red_flags
WHERE customer_number IN (SELECT customer_number
FROM red_flags
GROUP BY customer_number
HAVING COUNT(customer_number) > 1);
Execution plan:
1 PRIMARY leads ALL 80708 Using where
1 PRIMARY customers eq_ref PRIMARY PRIMARY 9 apcard_main.leads.customer_number 1
2 DEPENDENT SUBQUERY leads ALL 80708 Using where; Using temporary; Using filesort
2 DEPENDENT SUBQUERY customers eq_ref PRIMARY PRIMARY 9 apcard_main.leads.customer_number 1 Using index
DDL:
Table customers:
CREATE TABLE `customers` (
`customer_number` varchar(7) NOT NULL,
`dealer_id` varchar(32) DEFAULT NULL,
`first_name` varchar(64) DEFAULT NULL,
`middle_name` varchar(64) DEFAULT NULL,
`last_name` varchar(64) DEFAULT NULL,
`address_one` varchar(128) DEFAULT NULL,
`address_two` varchar(128) DEFAULT NULL,
`city` varchar(128) DEFAULT NULL,
`state` varchar(32) DEFAULT NULL,
`zip` char(5) DEFAULT NULL,
`fico` char(3) DEFAULT NULL,
`phone` varchar(13) DEFAULT NULL,
`mail_type` varchar(32) DEFAULT NULL,
`mail_date` date DEFAULT NULL,
`store` varchar(32) DEFAULT NULL,
PRIMARY KEY (`customer_number`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1$$
Table leads:
CREATE TABLE `leads` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
`dealer_id` varchar(32) DEFAULT NULL,
`customer_number` varchar(7) DEFAULT NULL,
`date` date DEFAULT NULL,
`time` time DEFAULT NULL,
`source` varchar(32) DEFAULT NULL,
`source_type` varchar(32) DEFAULT NULL,
`home_phone` varchar(13) DEFAULT NULL,
`email` varchar(64) DEFAULT NULL,
`mail_type` varchar(8) DEFAULT NULL,
`store` varchar(16) DEFAULT NULL,
`first_name` varchar(64) DEFAULT NULL,
`last_name` varchar(64) DEFAULT NULL,
`middle_name` varchar(32) DEFAULT NULL,
`city` varchar(128) DEFAULT NULL,
`state` varchar(32) DEFAULT NULL,
`zip` char(5) DEFAULT NULL,
`fico` char(3) DEFAULT NULL,
`mail_date` date DEFAULT NULL,
`work_phone` varchar(13) DEFAULT NULL,
`cell_phone` varchar(13) DEFAULT NULL,
`address_one` varchar(128) DEFAULT NULL,
`address_two` varchar(128) DEFAULT NULL,
`comment` varchar(255) DEFAULT NULL,
`caller_id` varchar(128) DEFAULT NULL,
PRIMARY KEY (`lead_id`)
) ENGINE=MyISAM AUTO_INCREMENT=125587 DEFAULT CHARSET=latin1$$
View red_flags:
CREATE
ALGORITHM=UNDEFINED
DEFINER=`apcard`#`97.83.30.118`
SQL SECURITY DEFINER
VIEW `red_flags` AS
select
`leads`.`dealer_id` AS `dealer_id`,
`customers`.`phone` AS `phone`,
`leads`.`date` AS `date`,
`leads`.`time` AS `time`,
`leads`.`source` AS `source`,
`leads`.`customer_number` AS `customer_number`,
`leads`.`caller_id` AS `caller_id`,
`leads`.`mail_type` AS `mail_type`,
`leads`.`store` AS `store`,
`leads`.`last_name` AS `last_name`,
`leads`.`first_name` AS `first_name`,
`leads`.`city` AS `city`,
`leads`.`state` AS `state`,
`leads`.`zip` AS `zip`,
`leads`.`fico` AS `fico`,
`leads`.`mail_date` AS `mail_date`,
`leads`.`home_phone` AS `home_phone`,
`leads`.`email` AS `email`
from (`customers` join `leads`)
where ((`customers`.`customer_number` = `leads`.`customer_number`)
and (`leads`.`date` >= (now() - interval 30 day)))$$
Have you tried joining rather than using a subquery?
SELECT rf.*
FROM red_flags rf
inner join red_flags rf2
on (rf2.customer_number = rf.customer_number)
group by rf2.customer_number
having count(rf2.customer_number) >1
I figured it out, I had a poorly formed date filter that was causing it to break when querying with an IN for some reason.
leads.date >= now() - INTERVAL 30 DAY