I have two tables, candidates and candidate_subjects, for storing candidate details and candidate scores respectively. I want a query to update candidate remark to 'FAIL' if the candidate pass less than 6 subjects. To pass a subject, the candidate sum of ca_score and exam_score for a subject must be greater than 40.
Below is the query I have written but it is not giving the result expected:
UPDATE candidates SET candidates.remark='FAIL' WHERE (select
count(candidate_subjects.id) AS total_pass from candidates,
candidate_subjects where candidates.id=candidate_subjects.candidate_id
and (candidate_subjects.ca_score + candidate_subjects.exam_score) >= 40) < 6
The tables:
CREATE TABLE candidate_subjects (
id INT(10) PRIMARY KEY NOT NULL AUTO_INCREMENT,
candidate_id INT(11),
exam_type_id INT(10),
subject_id INT(10),
ca_score INT(11),
exam_score INT(6),
score_grade VARCHAR(10),
date_created VARCHAR(10),
date_modified TIMESTAMP
);
INSERT INTO `candidate_subjects` (`id`, `candidate_id`, `exam_type_id`,
`subject_id`, `ca_score`, `exam_score`, `score_grade`, `date_created`,
`date_modified`) VALUES
(1, 2, 1, 32, 22, 61, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(2, 2, 1, 5, 21, 38, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(3, 2, 1, 14, 21, 51, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(4, 2, 1, 1, 19, 34, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(5, 2, 1, 2, 23, 39, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(6, 2, 1, 38, 20, 32, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(7, 2, 1, 53, 24, 47, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(8, 4, 1, 32, 19, 61, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(9, 4, 1, 5, 22, 41, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(10, 4, 1, 14, 20, 46, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(11, 4, 1, 1, 23, 37, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(12, 4, 1, 2, 21, 36, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(13, 4, 1, 38, 22, 34, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(14, 4, 1, 53, 24, 52, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(15, 5, 1, 32, 20, 62, NULL, '2017-02-01', '2017-08-28 13:11:44'),
(16, 5, 1, 5, 22, 38, NULL, '2017-02-01', '2017-08-28 13:11:44');
CREATE TABLE candidates (
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
exam_no VARCHAR(15),
surname VARCHAR(50),
other_names VARCHAR(100),
school_id INT(11),
registration_completed INT(11),
exam_scores_completed INT(5),
remark VARCHAR(10)
);
INSERT INTO candidates (id, exam_no, surname, other_names, school_id,
registration_completed, exam_scores_completed, remark) VALUES
(1, '1171052001', 'ABADO', 'MASENENGEN', 1052, 1, '1', ''),
(2, '1170938001', 'AGBA', 'NGUHER', 938, 1, '1', ''),
(3, '1170071001', 'ABEE', 'SESUUR', 71, 1, '1', ''),
(4, '1170938002', 'AHEN', 'REBECCA DOOSUUN', 938, 1, '1', '');
To update your candidates according to your criteria and their records exists in candidate_subjects you can use below query.
UPDATE candidates c
JOIN (select
count(id) AS total_pass ,candidate_id
from candidate_subjects
where(ca_score + exam_score) >= 40
group by candidate_id
) a on c.id = a.candidate_id
SET c.remark='FAIL'
WHERE total_pass < 6
DEMO
Can you try something like:
UPDATE candidates SET candidates.remark='FAIL' WHERE candidate_id IN (SELECT candidate_id FROM candidate_subject WHERE candidatescorecondition>40 HAVING COUNT(*) > 6);
Please run in a dev environment first!
If candidates can be marked as FAIL without doing all the exams you can do something like this:
update candidates set remark='FAIL'
where id in (
select candidate_id from candidate_subjects
where ca_score+exam_score > 40 -- passed subject
group by candidate_id having count(*)<6
)
but if a candidate must have data for at least six subjects in order to be evaluated you need to add an extra condition (so a candidate is not marked as failed if he/she is in the middle of the process):
update candidates set remark='FAIL'
where id in (
select candidate_id from candidate_subjects
where ca_score+exam_score > 40 -- passed subject
group by candidate_id having count(*)<6
)
and id in (
select candidate_id from candidate_subjects
group by candidate_id having count(*)>=6 -- the candidate has data for at leas 6 subjects
)
Related
I need to select IDs of products that have specific racketCondition id.
Table racket_attribute:
enter image description here
Table racket_condition:
enter image description here
Table racket_option:
enter image description here
My condition is: if the user select 1, 5, 10, and 21 which will get exactly product_id 16 but if the select 1, 5, 10, 20 then it will pot up 'no data found' or find the which product id which fulfills the racketCondition id.
racket_option SQL script:
CREATE TABLE racket_option (
racketOption_id int(11) NOT NULL,
product_id int(11) NOT NULL,
racketCondition_id int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table racket_option
INSERT INTO racket_option (racketOption_id, product_id, racketCondition_id) VALUES
(1, 16, 1),
(2, 16, 5),
(3, 16, 10),
(5, 21, 1),
(6, 21, 5),
(7, 23, 1),
(8, 23, 2),
(9, 27, 1),
(10, 28, 1),
(11, 29, 1),
(12, 16, 21),
(13, 21, 20),
(14, 23, 20),
(15, 27, 20),
(16, 28, 20),
(17, 30, 21),
(18, 30, 7),
(19, 30, 11),
(20, 21, 8),
(21, 16, 8);
--
-- Indexes for dumped tables
--
-- Indexes for table racket_option
ALTER TABLE racket_option
ADD PRIMARY KEY (racketOption_id),
ADD KEY racketCondition_FK (racketCondition_id),
ADD KEY productID_FK4 (product_id);
I have a table like this
CREATE TABLE `users_search_activity` (
`ID` bigint(20) UNSIGNED NOT NULL,
`user_id` int(11) NOT NULL,
`country_id` int(11) NOT NULL,
`search_keywords` text COLLATE utf8mb4_unicode_ci NOT NULL,
`date` datetime NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Dumping data for table `users_search_activity`
--
INSERT INTO `users_search_activity` (`ID`, `user_id`, `country_id`, `search_keywords`, `date`) VALUES
(1, 132, 2, 'xavie', '2021-07-13 08:20:37'),
(2, 132, 6, 'xavier', '2021-07-13 08:21:38'),
(3, 132, 5, 'xavier ins', '2021-07-13 08:21:39'),
(4, 132, 4, 'xavier ins', '2021-07-13 08:21:39'),
(5, 131, 9, 'xavier ins', '2021-07-13 08:22:12'),
(6, 132, 7, 'xavier ins', '2021-07-13 08:22:25'),
(7, 132, 8, 'xavier ins', '2021-07-13 09:24:43'),
(8, 132, 6, 'xavier ins', '2021-07-13 09:24:45'),
(9, 132, 4, 'xavier insa', '2021-07-13 09:24:47'),
(10, 131, 5, 'ins', '2021-07-13 09:24:54'),
(11, 132, 3, 'ins', '2021-07-13 09:24:54'),
(12, 132, 2, 'ins', '2021-07-13 09:24:58'),
(13, 132, 9, 'ins', '2021-07-13 09:24:59'),
(14, 132, 3, 'ins', '2021-07-13 09:25:00'),
(15, 132, 4, 'ins', '2021-07-13 09:25:02'),
(16, 132, 5, 'inst', '2021-07-13 09:58:20'),
(17, 132, 9, 'inst', '2021-07-04 09:58:25'),
(18, 132, 100, 'inst', '2021-07-07 09:58:25'),
(19, 132, 1, 'inst', '2021-07-11 09:58:26'),
(20, 132, 4, 'inst', '2021-07-12 09:58:26'),
(21, 1, 12, 'University Business Academy in Novi Sad', '2021-07-14 10:16:33');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users_search_activity`
--
ALTER TABLE `users_search_activity`
ADD PRIMARY KEY (`ID`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users_search_activity`
--
ALTER TABLE `users_search_activity`
MODIFY `ID` bigint(21) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=21;
COMMIT;
Here I want to get the count of users based on country_id and date. So to achieve that I have made sql query like this
SELECT COUNT(user_id) as count, date(date) as date , country_id
FROM users_search_activity
WHERE country_id != ""
GROUP BY country_id
ORDER BY date DESC
Up to some extent its working fine but the issue is its not getting the row for 2021-07-12 date.
Here is the output what I am getting
count date country_id
1 2021-07-14 12
3 2021-07-13 5
4 2021-07-13 4
3 2021-07-13 9
1 2021-07-13 7
1 2021-07-13 8
2 2021-07-13 3
2 2021-07-13 2
2 2021-07-13 6
1 2021-07-11 1
1 2021-07-07 100
Can someone tell me what I am doing wrong here? Any suggestions and advice would be really appreciable.
FYI: I am using MySQL 5.7
I am trying to create a database, but when I run this code, I get this error:
Code 1215: “Cannot add foreign key constraint"
Code:
drop database if exists kat_db;
create database if not exists kat_db;
use kat_db;
create table if not exists Patients
(
PatientID int not null PRIMARY KEY,
PatientName varchar(50),
TypeOfAnimal varchar(50),
DateOfLastApt date,
PatientAge int,
Microchipped boolean,
OwnerID int not null,
DoctorID int not null,
foreign key (OwnerID) references Owners(OwnerID),
foreign key (DoctorID) references Doctors(DoctorID)
);
create table if not exists Owners
(
OwnerID int not null PRIMARY KEY,
OwnerFirstName varchar(50),
OwnerLastName varchar(50),
PatientID int,
OwnerPhone int,
OwnerZip int,
PreferredPaymentMethod varchar(50),
foreign key (PatientID) references Patients(PatientID)
);
create table if not exists Doctors
(
DoctorID int not null PRIMARY KEY,
DoctorFirstName varchar(50),
DoctorLastName varchar(50),
PatientID int,
DoctorPhone int,
DoctorZip int,
AnimalSpecialty varchar(50),
foreign key (PatientID) references Patients(PatientID)
);
insert into Patients (PatientID, TypeOfAnimal, PatientAge, PatientName, DateOfLastApt, Microchipped, DoctorID, OwnerID)
values (01, 'dog', 6, 'Dima', '2017-02-05', 1, 101, 1001),
(02, 'dog', 3, 'Misha', '2017-02-05', 1, 102, 1002),
(03, 'cat', 2, 'Pistol', '2017-06-09', 0, 103, 1003),
(04, 'cat', 1, 'Nessie', '2017-09-04', 0, 104, 1004),
(05, 'cat', 2, 'Charlie', '2016-06-04', 1, 105, 1005),
(06, 'cat', 12, 'Francis', '2015-05-07', 1, 106, 1006),
(07, 'rabbit', 5, 'Bunny', '2015-05-06', 0, 107, 1007),
(08, 'turtle', 6, 'Aqua', '2017-06-08', 0, 108, 1008),
(09, 'dog', 16, 'Sammie', '2012-09-07', 1, 109, 1009),
(10, 'dog', 14, 'Dog', '2016-04-06', 0, 110, 1010);
insert into Owners (OwnerID, OwnerFirstName, OwnerLastName, OwnerPhone, OwnerZip, PreferredPaymentMethod, PatientID)
values (101, 'Kat', 6, 'Dima', '2017-02-05', 1, 101, 1001),
(102, 'Hunter', 3, 'Misha', '2017-02-05', 1, 102, 1002),
(103, 'Vicky', 2, 'Pistol', '2017-06-09', 0, 103, 1003),
(104, 'Vanessa', 1, 'Nessie', '2017-09-04', 0, 104, 1004),
(105, 'Weston', 2, 'Charlie', '2016-06-04', 1, 105, 1005),
(106, 'Jonas', 12, 'Francis', '2015-05-07', 1, 106, 1006),
(107, 'Devin', 5, 'Bunny', '2015-05-06', 0, 107, 1007),
(108, 'Grego', 6, 'Aqua', '2017-06-08', 0, 108, 1008),
(109, 'Jackson', 16, 'Sammie', '2012-09-07', 1, 109, 1009),
(110, 'dog', 14, 'Dog', '2016-04-06', 0, 110, 1010);
insert into Patients (PatientID, TypeOfAnimal, PatientAge, PatientName, DateOfLastApt, Microchipped, DoctorID, OwnerID)
values (01, 'dog', 6, 'Dima', '2017-02-05', 1, 101, 1001),
(02, 'dog', 3, 'Misha', '2017-02-05', 1, 102, 1002),
(03, 'cat', 2, 'Pistol', '2017-06-09', 0, 103, 1003),
(04, 'cat', 1, 'Nessie', '2017-09-04', 0, 104, 1004),
(05, 'cat', 2, 'Charlie', '2016-06-04', 1, 105, 1005),
(06, 'cat', 12, 'Francis', '2015-05-07', 1, 106, 1006),
(07, 'rabbit', 5, 'Bunny', '2015-05-06', 0, 107, 1007),
(08, 'turtle', 6, 'Aqua', '2017-06-08', 0, 108, 1008),
(09, 'dog', 16, 'Sammie', '2012-09-07', 1, 109, 1009),
(10, 'dog', 14, 'Dog', '2016-04-06', 0, 110, 1010);
I tried creating the tables in different order. I also created a different database, which worked, In fact, I used this to create the database on the top.
drop database if exists kat3_db;
create database if not exists kat3_db;
use kat3_db;
create table if not exists Book(
BookNumber int not null,
BookName varchar(50),
BookPrice decimal(5,2),
CoverType varchar(15) default 'hardcover',
PublicationDate date,
primary key (BookNumber)
);
create table if not exists Course(
CourseNo int not null,
CourseName varchar(20),
Semester varchar(10),
BookNumber int not null,
primary key (CourseNo),
foreign key (BookNumber) references Book(BookNumber)
);
insert into Book
(BookNumber, BookName, BookPrice, CoverType, PublicationDate)
values
(1, 'Math', 023.64, 'paperback', '1999-02-05'),
(3, 'English', 999.36, 'e-book', '2013-06-04'),
(5, 'Calc', 056.69, 'paperback', '1964-05-05'),
(6, 'C++', 053.98, 'paperback', '2016-03-04'),
(7, 'Programming', 113.02, 'paperback', '2001-10-11'),
(9, 'Art', 250.99, 'paperback', '1996-11-11'),
(10, 'Networking', 036.64, 'e-book', '2014-05-06'),
(12, 'Drawing', 111.36, 'paperback', '2013-06-04'),
(13, 'Robotics', 012.03, 'e-book', '2016-06-05'),
(14, 'Computer', 25.03, 'e-book', '2001-06-04')
;
insert into Book
(BookNumber, BookName, BookPrice, PublicationDate)
values
(2, 'Reading', 364.20, '2016-06-05'),
(4, 'Database', 036.64, '2017-06-05'),
(8, 'Wellness', 050.00, '1999-10-10'),
(11, 'Linux', 55.36, '2013-06-08'),
(15, 'FYE', 03.01, '1991-02-05')
;
insert into Course
(CourseNo, CourseName, Semester, BookNumber)
values
(111, 'IntroToMath', 'Spring', 1),
(222, 'IntroToReading', 'Fall', 2),
(333, 'IntroToEnglish', 'Spring', 3),
(444, 'IntroToDatabase', 'Fall', 4),
(555, 'IntroToCalc', 'Spring', 5),
(666, 'IntroToC++', 'Spring', 6),
(777, 'IntroToProgramming', 'Fall', 7),
(888, 'IntroToWellness','Spring', 8),
(999, 'IntroToArt', 'Fall', 9),
(010, 'IntroToNetworking', 'Spring', 10),
(011, 'IntroToLinux', 'Fall', 11),
(012, 'IntroToDrawing', 'Spring', 12),
(013, 'IntroToRobotics', 'Fall', 13),
(014, 'IntroToComputer', 'Spring', 14),
(015, 'IntroToFYE', 'Summer', 15)
;
I do not see what I did differently. Any suggestions would be greatly appreciated. If someone can run the first code on top and tell me if it works for them or not, that would be big help also. Thank you!
At the time of creating Patients table, owner and Doctor table information is not available. So it is not allowing you.
So just create the table first.
and then add FK with alter table.
You're adding you tables in the wrong order.
You create the Patients table with foreign key constraints before you create the tables/fields they rely on to exist.
I want to filter property with two type multi checkbox :
1 - Option
2 - Type
Here is my tables for this type:
propety_type:
CREATE TABLE IF NOT EXISTS `property_type` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`PropertyNumber` int(4) NOT NULL,
`TypeNumber` int(50) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci AUTO_INCREMENT=25 ;
INSERT INTO `property_type` (`ID`, `PropertyNumber`, `TypeNumber`) VALUES
(13, 53, 1),
(14, 53, 2),
(15, 53, 3),
(16, 54, 3),
(17, 54, 5),
(18, 55, 6),
(19, 55, 8),
(20, 56, 3),
(21, 56, 2),
(22, 56, 1),
(23, 54, 1),
(24, 55, 1);
property_option:
CREATE TABLE IF NOT EXISTS `property_option` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`PropertyNumber` int(11) NOT NULL,
`OptionNumber` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ;
INSERT INTO `property_option` (`ID`, `PropertyNumber`, `OptionNumber`) VALUES
(35, 53, 1),
(36, 53, 2),
(37, 53, 3),
(39, 54, 3),
(40, 54, 5),
(41, 55, 6),
(42, 55, 8),
(43, 56, 2),
(45, 56, 1),
(46, 56, 3),
(47, 56, 8),
(48, 53, 9),
(49, 53, 4),
(50, 55, 1),
(51, 54, 2),
(52, 54, 1);
My query :
SELECT property.PropertyNumber
FROM property
INNER JOIN property_option ON property.PropertyNumber = property_option.PropertyNumber
WHERE property_option.OptionNumber IN (1,3 )
GROUP BY property.PropertyNumber
HAVING COUNT(DISTINCT property_option.ID) =2
INNER JOIN property_type ON property.PropertyNumber = property_type.PropertyNumber
WHERE property_type.TypeNumber IN (1,2 )
GROUP BY property.PropertyNumber
HAVING COUNT(DISTINCT property_type.ID) =2
But when i test this in phpmyadmin, i get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN property_option ON property.PropertyNumber = property_option.Property' at line 9
If you want to make sure the rows returned fulfills the requirement of having both specified options as well as both specified types then you can use the query below.
SELECT p.PropertyNumber
FROM property p
JOIN property_option USING (PropertyNumber)
JOIN property_type USING (PropertyNumber)
WHERE OptionNumber IN (1,8)
AND TypeNumber IN (1,2)
GROUP BY p.PropertyNumber
HAVING COUNT(DISTINCT OptionNumber) = 2
AND COUNT(DISTINCT TypeNumber) = 2;
Sample SQL Fiddle
First table
CREATE TABLE IF NOT EXISTS `city_node` (
`node_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`parent_node_id` int(10) unsigned NOT NULL DEFAULT '0',
`lft` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Nested set info ''left'' value',
`rgt` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Nested set info ''right'' value',
`depth` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Depth = 0: no parent',
PRIMARY KEY (`node_id`),
KEY `parent_node_id` (`parent_node_id`),
KEY `lft` (`lft`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=26;
And data
INSERT INTO `city_node` (`node_id`, `title`, `parent_node_id`, `lft`, `rgt`, `depth`) VALUES
(1, 'Great Britain', 0, 1, 20, 0),
(3, 'England', 1, 2, 9, 1),
(7, 'Scotland', 1, 16, 19, 1),
(8, 'Edinburgh', 7, 17, 18, 2),
(9, 'Wales', 1, 10, 15, 1),
(10, 'Cardiff', 9, 11, 12, 2),
(11, 'London', 3, 3, 4, 2),
(12, 'Birmingham', 3, 5, 6, 2),
(13, 'Germany', 0, 21, 26, 0),
(14, 'Stuttgart', 13, 22, 23, 1),
(15, 'Newport', 9, 13, 14, 2),
(16, 'Munich', 13, 24, 25, 1),
(17, 'Israel', 0, 27, 32, 0),
(18, 'Tel Aviv', 17, 28, 29, 1),
(19, 'Ashdod', 17, 30, 31, 1),
(20, 'USA', 0, 33, 38, 0),
(21, 'New York', 20, 34, 35, 1),
(24, 'Liverpool', 3, 7, 8, 2),
(25, 'Detroit', 20, 36, 37, 1);
Second table
CREATE TABLE IF NOT EXISTS `city_node_entity` (
`node_id` int(10) NOT NULL,
`entity` tinyint(3) unsigned NOT NULL DEFAULT '1',
KEY `node_id` (`node_id`,`entity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And data
INSERT INTO `city_node_entity` (`node_id`, `entity`) VALUES
(11, 1),
(12, 1),
(16, 1),
(19, 1);
I want to get node with entity 1 and its ancestors, like this
Great Britain
--England
----London
----Birmingham
Germany
--Munich
Israel
--Ashdod
So, my query is
SELECT DISTINCT(node_ext.node_id), node_ext.*
FROM city_node_entity AS entity
LEFT JOIN city_node AS node
ON entity.node_id = node.node_id
LEFT JOIN city_node AS node_ext
ON node_ext.lft <= node.lft AND node_ext.rgt >= node.rgt
WHERE entity.entity = 1
ORDER BY node_ext.lft
But explain shows
-Using where; Using index; Using temporary; Using filesort
Is there any other queries to get same result but with less [EXTRA]?
The only thing you can do for recursive queries is to run it with joins for each parent/child.... because your second table is the actual city name and you want to work backwards you can do it like this... just add more joins till all results are null and you will have yourself the full tree
SELECT node.title AS child, t2.title as parent1, t3.title as parent2, t4.title as parent3
FROM city_node_entity AS entity
LEFT JOIN city_node AS node ON entity.node_id = node.node_id
LEFT JOIN city_node AS t2 ON t2.node_id = node.parent_node_id
LEFT JOIN city_node AS t3 ON t3.node_id = t2.parent_node_id
LEFT JOIN city_node AS t4 ON t4.node_id = t3.parent_node_id;
Fiddle Demo