Cursor expression in MySQL - mysql

How do I achieve the same as an Oracle cursor expression does in a MySQL database (version from 5.6 onwards)
below is a sample query of an Oracle cursor expression
SELECT department_name, CURSOR(SELECT salary, commission_pct
FROM employees e
WHERE e.department_id = d.department_id)
FROM departments d;
How can I achieve the same as this with a MySQL database?
if i execute this query on oracle below output i will be produced,
depart_name cursor result
MCA { < SALARY=20000 , COMMISSION_PCT=2 > , < SALARY=40000,COMMISSION_PCT=20> ,}
BE {< SALARY=20000,COMMISSION_PCT=2 >,}

I don't know what a CURSOR() does in oracle because I've never touched oracle, but I don't know if it would help you but I think you wanted to join like this:
SELECT d.department_name, e.salary, e.commission_pct.
FROM departments d
INNER JOIN employees e
ON (e.department_id = d.department_id);
I give you this link for more information on joints:
https://sql.sh/cours/jointures
and according to sql.sh:
There are several methods to associate 2 tables together. Here is the
list of the different techniques that are used:
INNER JOIN: internal join to return the records when the condition is true in both tables. This is one of the most common
joins.
CROSS JOIN: cross join to make the Cartesian product of 2 tables. In other words, allows to join each row of a table with each
row of a second table. Attention, the number of results is
generally very high.
LEFT JOIN (or LEFT OUTER JOIN): external join to return all the records of the left table (LEFT = left) even if the condition is not
checked in the other table.
RIGHT JOIN (or RIGHT OUTER JOIN): External join to return all records in the right-hand table (RIGHT = right) even if the condition
is not checked in the other table.
FULL JOIN (or FULL OUTER JOIN) : external join to return the results when the condition is true in at least one of the 2 tables.
SELF JOIN : allows to join a table with itself as if it were another table.
NATURAL JOIN : natural join between 2 tables if there is at least one column with the same name between the 2 SQL tables.
UNION JOIN: joint of union.
if you have any questions, I am available to answer them.

Related

Full outer Join mySQL - missing rows

I am trying to emulate a full outer join in mySQL by using a left join and unioning it with a right join.
SELECT * FROM auxview_candiNotes AS a
LEFT JOIN auxview_clientNotes AS b ON a.dateAdded = b.dateAdded
UNION
SELECT * FROM auxview_candiNotes AS a
RIGHT JOIN auxview_clientNotes AS b ON a.dateAdded = b.dateAdded
The reason I am doing this is because I am joining two tables and occasionally one of them will not have a value on a specific date (thus the date is not listed in the table). However the joined table should display all dates, that occur in either or both of the tables. If one of the tables does not contain a value for a specific date, it should return NULL.
However, some dates from the auxview_clientNotes table are not listed in the joined table.
Thanks in advance!
This method isn't exactly a full outer join, because the union removes duplicates. Duplicates can be generated when the underlying tables have duplicates. Often duplicate removal is considered a "good" think, but the method is not exactly equivalent to a full outer join.
I prefer a left join approach:
select candi.*, client.*
from (select dateAdded from auxview_candiNotes union -- intentionally not `union all`
select dateAdded from auxview_clientNotes
) d left join
auxview_candiNotes candi
on candi.dateAdded = d.dateAdded left join
auxview_clientNotes client
on client.dateAdded = d.dateAdded;

Full Outer Joins in MS Access

My situation: I have a central PI table (patient information) which stores every single patient in the database. Then I have a few other tables (ex. vital signs, self report scores, etc.). The problem is, not every patient completes measures in every table. I.e., a patient might have an entry in the vital signs table but not in the self report table. How can I run a query where it shows me ALL patients in the PI table and their corresponding vitals and SR data if it exists instead of restricting the query to only show me patients that have records in ALL three of those tables?
I'm unclear why you want a FULL OUTER JOIN.
You have a PI table which includes a row for every patient. You have another table like SR data which contains the self-reported data for those patients. Assuming they have a common key ... perhaps patient_id ... a LEFT JOIN will give you a row for each patient regardless of whether there is any SR Data available for that patient:
SELECT *
FROM
[PI table] AS pi
LEFT OUTER JOIN [SR Data] AS sr
ON pi.patient_id = sr.patient_id;
Note Access will be happy with either LEFT OUTER JOIN or just LEFT JOIN there. OUTER is not needed, but Access won't object if you include it.
So I think you only need a LEFT JOIN. You would only need (the equivalent of) a FULL OUTER JOIN if the SR Data includes rows which don't match to any of the existing patients in the PI table and you want those unmatched rows included in the query result. But you didn't indicate that applies to your question, so I'm ruling it out unless you tell us otherwise.
You can LEFT JOIN another table to the first query ...
SELECT *
FROM
([PI table] AS pi
LEFT OUTER JOIN [SR Data] AS sr
ON pi.patient_id = sr.patient_id)
LEFT JOIN [Vital Signs] AS v
ON pi.patient_id = v.patient_id;
... and extend from there for additional tables as needed. If you use the Access query designer to set up your joins, it will add in parentheses correctly as the db engine demands for any query which includes more than one join.
Access (unfortunately) does not support OUTER JOIN. What it does is INNER, LEFT and RIGHT JOINS.
INNER JOIN - Gives you only the information common to two tables.
RIGHT JOIN - Gives all information that are common to the two table and information that are not matched form the table that is on the RIGHT to the JOIN statement.
LEFT JOIN - Gives all information that are common to the two table and information that are not matched form the table that is on the LEFT to the JOIN statement.
OUTER JOIN - is the UNION of RIGHT JOIN and LEFT JOIN.
So as expected, you just have to perform two Queries that perform a JOIN in both directions and then marry them using a UNION. This is a long process, but is the only way !
SELECT * FROM
(SELECT table1.FieldName1, table2.FieldName2
FROM table1 RIGHT JOIN table2 ON table1.ID = table2.ID
UNION
SELECT table1.FieldName1, table2.FieldName2
FROM table1 LEFT JOIN table2 ON table1.ID = table2.ID)

SQL Join: Are selects between more than 2 tables still joins?

When I read about inner or outer joins in SQL, all examples and descriptions are about 2 tables being joined. What if there are more than 2 tables in the query? Is that still considered a join?
I think inner join still makes sense even if it is between multiple tables; but I'm not sure outer joins makes sense between more than 2 table.
Can someone please clarify this issue?
Inner joins and outer joins are perfectly reasonable to use with more than 2 tables.
Inner joins force the result to display only data that has whatever row you joined on, whereas outer joins display all data no matter what.
Let us say you wanted to join 4 tables together...
select * from testtable
inner join testable2 on col1 = othercolumn
inner join testable3 on col2 = othercolumn
leftjoin testable4 on col3 = othercolumn
In this case, it would return only results that existed in the inner joins, but the result would not have to exist in the outside/left join. You are forcing testtables 2 & 3 to have a value on what you are joining on.. it cannot be null.
The left join does not care if the value is null, and will show results anyway.
I hope this helps some... Basically.. if you inner join on a value, and it can possibly be null, then the entire query will show blank. This is the scenario you would use an outter join.. you are not forcing the value to exist.
Most examples of joins will include two tables. However, joins can be done on any number of tables.
You can read more about joins all over the interwebs, but you might want to start with:
http://www.w3schools.com/sql/sql_join.asp
http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
The w3schools article first thing stated is:
SQL joins are used to combine rows from two or more tables.
This isn't entirely true, as you can even join tables on themselvees!
Consider:
Employees
-----
EmployeeId
ManagerId
EmployeeName
if you want to find out the employees of a specific manager, that could be written as:
select manager.EmployeeName, subordinates.*
from employees manager
inner join employees subordinates on manager.employeeId = subordinates.managerId
For multiple table joins consider:
Employees
----
EmployeeId
ManagerId
EmployeeName
Departments
----
DepartmentId
DepartmentName
EmployeeDepartments
----
DepartmentId
EmployeeId
In this case, if you wanted to find out all department names that employee 5 belonged too, you could do:
select d.DepartmentName
from employees e
inner join employeeDepartments ed on e.employeeId = ed.employeeId
inner join departments d on ed.departmentId = d.departmentId
where e.employeeId = 5
TLDR; - yes including more than 2 tables is still considered join(s)
Every join clause is (logically) between two virtual tables but the virtual tables themselves can be defined as joins on further tables.
So in the following example
SELECT Foo
FROM A
INNER JOIN B
ON A.X = B.X
INNER JOIN C
ON C.Y = A.Y
AND C.Z = B.Z
it can be considered that logically A joins to B then the virtual table (A x B) is joined to C. Columns from all three of those tables are thus available in the final ON clause.
You can control the virtual tables that are evaluated by the placement of the ON clause.
The following example creates a virtual table (A x B) and a virtual table (C x D) and then joins these two together.
SELECT Foo
FROM A
INNER JOIN B
ON A.X = B.X
INNER JOIN C
INNER JOIN D
ON C.Y = D.Y /*Only C and D in scope here*/
ON A.Z = D.Z /*All tables back in scope*/
The query optimiser is free to actually implement the joins in any way that maintains the semantics however. As inner joins are commutative and associative the tables in the above example can be freely re-arranged. For outer joins re-arranging them could change the semantics.

How to create the inner query wtih 5 different tables mysql

Having 5 tables
Table a_dates = id,
Table b_types = id, a_date_id, c_type_id,
Table c_types = id, name,
Table d_profiles = id, name, profile_type
Table e_ps = id, a_date_id, d_profile_id
From a_dates Need to get b_types,...then from b_types needs c_types name,... Then compare c_types name with d_profiles name and get d_profiles id.... if equals then create a records in e_ps with a_date_id, d_profile_id.
Could any one please help me in getting the query from inner join.
I tried like, it is incomplete query
INSERT INTO e_ps(id,a_date_id,a_date_type,d_profile_id,c_id)
SELECT '',a.id,'A',dp.id,'67' FROM d_profiles dp
INNER JOIN a_dates a ON {HERE I NEED NAME MATCHING WITH c_types name} = dp.name and dp.profile_type = 'A'
INNER JOIN a_dates ON a.id = a_dates.id
LEFT OUTER JOIN e_ps eps ON eps.a_date_type = 'A' AND eps.a_date_id = a_dates.id
WHERE eps.a_date_id IS NULL
This seems to be a relatively simple JOIN:-
INSERT INTO e_ps(id, a_date_id, d_profile_id)
SELECT NULL, a_dates.id, d_profiles.id
FROM a_dates
INNER JOIN b_types ON a_dates.id = b_types.a_date_id
INNER JOIN c_types ON b.c_type_id = c.id
INNER JOIN d_profiles ON c_types.name = d_profiles.name
With joins there are several types, and I suspect you are getting confused. Briefly:-
With an INNER JOIN it looks for a match that is on BOTH tables. If no
match the no record is returned.
With a LEFT OUTER JOIN it takes a record from the table on the left
and looks for a match on the table on the right. If a match great,
but if not then it still brings a row back but the columns from the
table on the right just have values of NULL.
A RIGHT OUTER JOIN is very much the same, just with the tables
reversed (most people including me avoid using this as it has no
advantages most of the time but just makes things confusing).
With a FULL OUTER JOIN it gets the records from both side, whether
they match or not. If they match then the columns from both are
returned, if not matched then the columns from one are returned. Not
that MySQL does not support a FULL OUTER JOIN (although there are
ways to emulate it).
A CROSS JOIN joins every combination of 2 tables. These are used when
there is no common column to match on but you want all combinations.
For example if you wanted a table of all employees and all days of
the week for each employee you would cross join a table of days of
the week against a table of employees (then for useful data you might
LEFT OUTER JOIN a table of holidays to the result).

MySQL Join from multiple tables with GROUP_CONCAT

I am trying to select data from 3 separate tables, where 2 of those contains multiple rows of data. The sql query i am using is this, but i am experiencing that when i run it, if either taskdependees or tasksdependencies have zero result, the whole task is not showing.
SELECT t.*, GROUP_CONCAT(a.DependenciesId) as DiesId, GROUP_CONCAT(b.DependeesId) as DeesId FROM tasks t JOIN tasksdependencies a ON a.TasksId=t.TasksId JOIN taskdependees b ON b.TasksId=t.TasksId GROUP BY t.TasksId
What am I doing wrong in this query?
Use LEFT JOIN , inner join will give row if there is association is present in both tables while left will return the rows from left table even if they are not associated
SELECT t.*, GROUP_CONCAT(a.DependenciesId) as DiesId,
GROUP_CONCAT(b.DependeesId) as DeesId
FROM tasks t
LEFT JOIN tasksdependencies a ON a.TasksId=t.TasksId
LEFT JOIN taskdependees b ON b.TasksId=t.TasksId
GROUP BY t.TasksId