mysql join, need suggestion - mysql

I have created a MySQL table with a foreign key value. I want to use MySQL join to fetch foreign key value.
I have a table called employee and foreign key value sex, Sex table contain Male & Female.
this is my simple join query which is working:
SELECT * FROM sex JOIN employee ON employee.sex_id=sex.id
but i want to use join query here, but it seems I am missing some thing, Please complete this:
SELECT employee_id, first_name, last_name, sex_id FROM employee
And please tell me how i will insert to forigen key in single query for this table:
CREATE TABLE IF NOT EXISTS `employee` (
`employee_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) NOT NULL,
`middle_name` varchar(20) DEFAULT NULL,
`last_name` varchar(20) NOT NULL,
`employee_employee_id` int(11) DEFAULT NULL,
`address_id` int(11) DEFAULT NULL,
`phone_id` int(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`maritial_id` int(11) NOT NULL,
`sex_id` int(11) NOT NULL,
PRIMARY KEY (`employee_id`),
KEY `fk_employee_employee` (`employee_employee_id`),
KEY `fk_employee_address1` (`address_id`),
KEY `fk_employee_phone1` (`phone_id`),
KEY `fk_employee_maritial1` (`maritial_id`),
KEY `fk_employee_sex1` (`sex_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
I have not good idea in MySQL

SELECT emp.employee_id, emp.first_name, emp.last_name, emp.sex_id, sx.sex_label
FROM employee AS emp
JOIN sex AS sx
ON emp.sex_id = sx.id
Where sex_label is a field name in sex table that would contain either Male or Female.

Try something like:
SELECT e.employee_id, e.first_name, e.last_name, s.id
FROM employee AS e
JOIN sex AS s
ON e.sex_id = s.id

Related

How to get record from table Named Employee with the Foreign Key named ManagerId reference itself empId

I am stuck in this problem in which I am trying to getting a record from Employee table which has five columns named EmpId, Name, Age, Post and ManagerId.
ManagerId is a foreign key and referencing table to Employee table of EmpId . It Means Manager is also an employee of company.
The Problem is I want to get a record that will contain empId, Name And Manager Name which is associated with an employee record. So what will be the mysql query for this. This is Table information :-
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`empId` int(10) unsigned NOT NULL auto_increment,
`Name` varchar(45) default NULL,
`Age` varchar(45) default NULL,
`Post` varchar(45) default NULL,
`managerId` int(10) unsigned default NULL,
PRIMARY KEY (`empId`),
KEY `FK_employee_1` (`managerId`),
CONSTRAINT `FK_employee_1` FOREIGN KEY (`managerId`) REFERENCES `employee` (`empId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
You can do this using Left Join with Self.
SELECT e1.empId, e1.Name,
e2.Name AS manager_name
FROM employee e1
LEFT JOIN employee e2
ON e1.managerId = e2.empId

Inner join 3 tables and calculate the average value on a field

There are 3 tables:
medics:
IDM (id_medic) (primary key)
1st name
2nd name
specialty
patients:
IDP (id_patient) (primary key)
name
DOB (date of birth)
visits:
id
id_medic
id_patient
I would like to find out the average age of patients for each specialty.
SELECT specialty, AVG(year(curdate()) - year(patients.DOB))
FROM medics, patients, visits
WHERE medics.IDM = visits.medics GROUP by specialty;
The query above shows me on each line the average of all patients.
Try it:
SELECT
Speciality,
ROUND(AVG(YEAR(NOW())-YEAR(DOB)),0) AS Years
FROM visits
INNER JOIN medics
ON visits.IdMedic = medics.Id
INNER JOIN patients
ON visits.IdPatient = patients.Id
GROUP BY Speciality
My tables:
CREATE TABLE `visits` (
`Id` int(11) NOT NULL,
`IdMedic` int(11) DEFAULT NULL,
`IdPatient` int(11) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `patients` (
`Id` int(11) NOT NULL,
`Name` varchar(45) DEFAULT NULL,
`DOB` date DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `medics` (
`Id` int(11) NOT NULL,
`Name` varchar(45) DEFAULT NULL,
`Speciality` varchar(45) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

MySQL filtering result from a foreign key that doesn't have contents

i have two tables:
CREATE TABLE IF NOT EXISTS `test_category` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`score_type` varchar(50) NOT NULL,
`sub_code` int(11) NOT NULL,
`duration` varchar(10) NOT NULL,
`status` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`_id`),
KEY `sub_code` (`sub_code`)
)
CREATE TABLE IF NOT EXISTS `questions` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`question` varchar(255) NOT NULL,
`correct_ans` varchar(255) NOT NULL,
`incorrect1` varchar(255) NOT NULL,
`incorrect2` varchar(255) NOT NULL,
`incorrect3` varchar(255) NOT NULL,
`test_cat` int(11) NOT NULL,
`image_location` varchar(50) NOT NULL,
PRIMARY KEY (`_id`),
KEY `test_cat` (`test_cat`)
)
ALTER TABLE `questions`
ADD CONSTRAINT `questions_ibfk_1` FOREIGN KEY (`test_cat`) REFERENCES `test_category` (`_id`);
so basically these two tables are related. questions table is related to test_category table through the foreign key test_cat from the table questions referenced to the _id of test_category. what i wan't to display is those entry from test_category that have a entries related to it from the questions table. if an entry from test_category doesn't have anything referenced to it from the questions table then it shouldn't be displayed.
select distinct test_category._id, test_category.score_type
from test_category join questions
where ??
that's the sql i have tried but i don't know how to filter it with where...
select distinct test_category._id, test_category.score_type from test_category
join questions
on 'questions.test_cat' = 'test_category._id'
select distinct test_category._id, test_category.score_type from test_category
join questions
on questions.test_cat = test_category._id
kudos to Nisha and Abhik Chakraborty

How to optimize this query as the in array seems to slow things down significantly

I am looking to find out the best way to optimize a query like this:
SELECT
a.ID,
a.ECPCodeID,
a.RegDate,
a.BusName,
a.City,
a.AccountNum,
b.ID as RepCodeID,
b.RepCode
FROM ECPs_Registration a,
Reps_Codes b
WHERE (SUBSTR(a.PostalCode,1,5)IN(SELECT
SUBSTR(Zip,1,5)
FROM Reps_Zip
WHERE RepCodeID = b.ID)
AND a.AccountNum NOT IN(SELECT
ShipTo
FROM Reps_ShipTo))
OR a.AccountNum IN(SELECT
ShipTo
FROM Reps_ShipTo
WHERE RepCodeID = b.ID)
ORDER BY b.RepCode,a.BusName,a.City
I know there are more factors involved such as indexes and such, I just am asking about the query part of it for now. Mainly, since I have to go through the Reps_ShipTo and Reps_Zip tables for tons of records. I thought about changing something like:
a.AccountNum NOT IN (SELECT ShipTo FROM Reps_ShipTo)
INTO
(SELECT count(*) FROM Reps_ShipTo WHERE a.AccountNum = ShipTo) = 0
Not sure if that is proper or if there is a better way. Any help would be appreciated. Thanks.
EDIT:
Schema:
CREATE TABLE IF NOT EXISTS `ECPs_Codes` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`ECPCode` char(4) NOT NULL,
PRIMARY KEY (`ID`),
KEY `ECPCode` (`ECPCode`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
CREATE TABLE IF NOT EXISTS `ECPs_Registration` (
`RegDate` datetime NOT NULL,
`ID` int(10) NOT NULL AUTO_INCREMENT,
`ECPCodeID` int(11) NOT NULL,
`FirstName` varchar(200) NOT NULL,
`LastName` varchar(200) NOT NULL,
`BusName` varchar(200) NOT NULL,
`Address` varchar(200) NOT NULL,
`Address2` varchar(200) NOT NULL,
`City` varchar(100) NOT NULL,
`Province` char(2) NOT NULL,
`Country` varchar(100) NOT NULL,
`PostalCode` varchar(10) NOT NULL,
`Email` varchar(200) NOT NULL,
`AccountNum` int(8) NOT NULL,
PRIMARY KEY (`ID`),
KEY `ECPCodeID` (`ECPCodeID`),
KEY `PostalCode` (`PostalCode`),
KEY `AccountNum` (`AccountNum`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `Reps_Codes` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(50) NOT NULL,
`RepCode` varchar(16) NOT NULL,
`AllAccess` tinyint(4) NOT NULL,
PRIMARY KEY (`ID`),
KEY `RepCode` (`RepCode`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `Reps_ShipTo` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`RepCodeID` int(11) NOT NULL,
`ShipTo` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ID`),
KEY `RepID` (`RepCodeID`),
KEY `ShipTo` (`ShipTo`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `Reps_Zip` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`RepCodeID` int(11) NOT NULL,
`Zip` varchar(10) NOT NULL,
PRIMARY KEY (`ID`),
KEY `RepCodeID` (`RepCodeID`),
KEY `Zip` (`Zip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
There are two things that massively hurt performance on your query.
You are joining two tables by combining multiple conditions, each needing subqueries
You're doing a join on two tables using SUBSTR(Zip,1,5)=SUBSTR(postalcode,1,5)
The logic behind your query seems to be something like:
For every ECPs_Registration find the matching record in Rep_Codes
using the following rules:
If there is a matching record in Reps_ShipTo, to for that registration, use that table to look it up (primary match)
If there isn't a matching record in Reps_ShipTo, seek through Reps_Zip for a matching RepCode by Zipcode-match (secondary)
Now if the above fully describes your situation, you should probably start off by redesigning your database.
The Reps_ShipTo table creates a 0:N relationship between ECPs_Registration and Rep_Codes. Such relations don't need an extra table - they can simply be stored as nullable foreign keys - in your case a RepCodeId in ECPs_Registration would do the trick, and would remove the entire Reps_ShipTo table from the database.
You should probably also create (yes, redundant) extra columns that only store the first 5 letters of the zip codes in both ECPs_Registration and Reps_Zip. This will allow simple equality matches instead of the SUBSTR-functions. Or, you might decide to do this match only once for every record, and store the result in above RepCodeId, which totally eliminates the dual join.
The following query assumes you for some reason don't want to or can't change your database:
SELECT
a.ID, a.ECPCodeID, a.RegDate, a.BusName, a.City, a.AccountNum,
CASE (b1.ID IS NOT NULL, b1.ID, b2.ID) as RepCodeID,
CASE (b1.ID IS NOT NULL, b1.RepCode, b2.RepCode) as MyRepCode
FROM ECPs_Registration a
LEFT JOIN Reps_ShipTo ON (Reps_ShipTo.Shipto=a.AccountNum)
LEFT JOIN Rep_Codes b1 ON (b1.ID=Reps_ShipTo.RepCodeId)
LEFT JOIN Reps_Zip ON (SUBSTR(Zip,1,5)=SUBSTR(a.postalcode,1,5))
LEFT JOIN Rep_Codes b2 ON (b2.ID=Reps_Zip.RepCodeID)
ORDER BY MyRepCode,a.BusName,a.City
Without your database schema and sample data, I have no way to test if above query actually works and has the same result as your original.
SELECT
a.ID,
a.ECPCodeID,
a.RegDate,
a.BusName,
a.City,
a.AccountNum,
b.ID as RepCodeID,
b.RepCode
FROM ECPs_Registration a, Reps_Codes b
INNER JOIN Reps_Zip as r on SUBSTR(a.PostalCode,1,5) = SUBSTR(r.Zip,1,5)
LEFT JOIN Reps_ShipTo as rs on a.AccountNum = rs.ShipTo
LEFT JOIN ShipTo as s on a.AccountNum = s.ShipTo
WHERE (s.id is null or rs.id is null)
ORDER BY b.RepCode,a.BusName,a.City

How can I select the current holder for each championship?

I want to select the current holders for each championship in a championships table, and return NULL for championships that have not had any winners yet.
Here are the create statements for the two tables:
CREATE TABLE `championships` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`friendly_name` varchar(255) NOT NULL,
`rank` int(2) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `friendly_name` (`friendly_name`)
) ENGINE=InnoDB;
CREATE TABLE `title_history` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`championship` int(10) unsigned NOT NULL,
`winner` varchar(255) NOT NULL,
`date_from` date NOT NULL,
`location` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `championship` (`championship`)
) ENGINE=InnoDB;
ALTER TABLE `title_history` ADD CONSTRAINT `title_history_ibfk_1` FOREIGN KEY (`championship`) REFERENCES `championships` (`id`) ON UPDATE CASCADE;
What MySQL statement would return the data set I wanted?
Assuming you're storing the winner of a championship as the primary key/id of the holder, something like this should work. You might want to add in another join to get the actual name of the team from another table though.
Because LEFT join will only select rows from the 'right' table when there is a match, everything that doesn't have one should come back as NULL.
SELECT name, [holder]
FROM championships AS c
LEFT JOIN title_history AS h ON c.winner = h.id
EDITED VERSION:
With further insight into your tables and from your comment, maybe try this subselect:
SELECT friendly_name,
(SELECT winner FROM title_history WHERE championship = c.id ORDER BY date_from DESC LIMIT 1)
FROM championships AS c
ORDER BY name
If I understand your structure correctly, that ought to get the last winner of each championship?