I try create a view with one table multi time here is my code but the result is wrong.Can you help me I cant see here wrong thing.
SELECT DISTINCT
O1.HomeWorkId, O2.FileInfo AS TeacherFileInfo,
O2.Answer AS TeacherAnswer, O1.Answer AS StudentAnswer,
O1.StudentId
FROM
HomeWorkAnswer AS O1
INNER JOIN
HomeWorkAnswer AS O2 ON O1.FileInfo = O2.FileInfo
WHERE
(O1.HomeWorkId > 0)
I save my teacher answer and student answer in this table.In my project I select two times for compare the answer.But I thing for performance it is not good and try create something like this.
You can try using derived tables to separate teachers answer from students answers.
SELECT DISTINCT
sa.HomeWorkId,
ta.FileInfo AS TeacherFileInfo,
ta.Answer AS TeacherAnswer,
sa.Answer AS StudentAnswer,
sa.StudentID
FROM
(SELECT *
FROM HomeWorkAnswer
WHERE studentId = -1) ta
JOIN
(SELECT *
FROM HomeWorkAnswer
WHERE teacherId = -1) sa ON ta.FileInfo = sa.FileInfo
Related
I have a table with Customers which includes their contact person in the helpdesk. I have another table that lists all vacancies of the helpdesk employees - if they are currently sick or on vacation etc.
I need to get the helpdesk contact and the start/end time of their vacation IF there is an entry.
I currently have this (simplified):
SELECT *
FROM dbo.Customers, dbo.Projects, dbo.Vacations
WHERE ($Phone = dbo.Customers.Phone)
AND dbo.Customers.CustomerID = dbo.Projects.CustomerID
AND dbo.Projects.HDContactID = dbo.Vacations.HDContactID
So if there is a vacation listed in the Vacations table, it works fine, but if there is no vacation at all, this will not return anything - what i want is that if there is no vacation, it simply returns the other data, and ignores the missing data (returns NULL, doesn't return anything, not important)
In any case, I need to get the Customers and Project data, even if the query can't find an entry in the Vacations table. How would I do this? I pretty new to SQL and couldn't find a similar question on this site
EDIT: I'm using SQL Server, currently using HeidiSQL
Try below query:
SELECT * FROM dbo.Customers, dbo.Projects
left join dbo.Vacations on dbo.Projects.HDContactID = dbo.Vacations.HDContactID
WHERE ($Phone = dbo.Customers.Phone)
AND dbo.Customers.CustomerID = dbo.Projects.CustomerID
Use left join as mentioned by #Flying Thunder,
Example of the left join:
SELECT country.country_name_eng, city.city_name, customer.customer_name
FROM customer
LEFT JOIN city ON customer.city_id = city.id
LEFT JOIN country ON city.country_id = country.id;
You can find a nice guide for the joins and SQL here:
https://www.sqlshack.com/learn-sql-join-multiple-tables/
You should be using LEFT JOIN. In fact, you should never be using commas in the FROM clause. That is just archaic syntax and closes the powerful world of JOINs from your queries.
I also recommend using table aliases that are abbreviations of table names. The best are abbreviations for the table names:
SELECT *
FROM dbo.Customers c LEFT JOIN
dbo.Projects p
ON c.CustomerID = p.CustomerID LEFT JOIN
dbo.Vacations v
ON p.HDContactID = v.HDContactID
WHERE c.Phone = $Phone;
Have you try this to skip vacation record if not present like this:
SELECT * FROM dbo.Customers, dbo.Projects, dbo.Vacations
WHERE ($Phone = dbo.Customers.Phone)
AND dbo.Customers.CustomerID = dbo.Projects.CustomerID
AND (dbo.Vacations.HDContactID IS NULL OR dbo.Projects.HDContactID = dbo.Vacations.HDContactID)
I have Four tables as follows
Review (REV_ID pk , REV_NAME)
Meeting (MEETING_ID pk,MEETING_NAME,REV_ID fk to Review)
Task (TASK_ID pk,TASK_NAME,REV_ID fk to Review)
Answer (ANS_ID pk,ANS_NAME,REV_ID fk to Review)
Now I want to select a particular Review and want to create a table with
Linked meetings
Linked answers
Linked tasks
How shall I proceed with it?
I tried writing join query but I was only able to get data if Rev_ID is present in all tables?
select * from
(SELECT *
FROM meeting
WHERE EXISTS (SELECT *
FROM review WHERE meeting.rev_id
=review.rev_id)
and meeting.rev_id=142),
(SELECT *
FROM answer
WHERE EXISTS (SELECT *
FROM review WHERE answer.rev_id
=rev.rev_id)
and answer.ans_rev_id=142),
(SELECT *
FROM task
WHERE EXISTS (SELECT *
FROM review WHERE task.rev_id
=review.rev_id)
and task.rev_id=142) r;
Note : Here I tried static Rev_ID =142 to check data.
From above query i am getting output only if data exist in all four table, But if Data does not exist in any table, It does not return remaining value.
I want at-least names of all table's in final output.
Try the following, let us know if this meets your requirements.
SELECT rv.rev_id,
rv.rev_name,
mt.meeting_name,
tk.task_name,
ans.ans_name
FROM review rv
LEFT OUTER JOIN meeting mt ON (rv.rev_id = mt.rev_id)
LEFT OUTER JOIN task tk ON (rv.rev_id = tk.rev_id)
LEFT OUTER JOIN answer ans ON (rv.rev_id = ans.rev_id)
WHERE rv.rev_id = 142
SQL Fiddle Demo
If the above SQL is fine, prefix it with create table or view syntax to combine them into one.
I want to create a view where all birthdays of all persons are listed. Lets say I want to "expand" a table.
I have a table Person with the attributes ID and Birthday.
I have a function FuncBirthdays that takes an ID and a Birthday and then it lists all birthdays until the current date. For example:
SELECT * FROM FuncBirthdays(123, '01.01.2014');
Result:
123; '01.01.2014'
123; '01.01.2015'
123; '01.01.2016'
// Current date is 14.01.2016, so the list stops here.
Now I want to create a view with all birthdays of all persons in table Person.
SELECT *
FROM
Person
INNER JOIN
(SELECT * FROM FuncBirthdays(Person.ID, Person.Birthday)) FB
ON Person.ID = FB.ID
The error message in the MS SQL Server 2012 Studio is that Person.ID and Person.Birthday are unbound.
I'm guessing that this way collides with the way JOINS are done. What SQL-concepts could I use to expand a table? Every data set in Person generates a variable amount of data sets in the output. Currently I have solved this problem with a C#.net function that collects the data manually. Is there a pure SQL way?
Thanks
Martin
"The error message in the MS SQL Server 2012 Studio is that Person.ID and Person.Birthday are unbound."
Is because of you are trying to access person in Join result set which can not be accessible. You need to use cross apply in that case, as per my knowledge. Please try following query
SELECT * FROM Person p
CROSS APPLY
(
SELECT * FROM FuncBirthdays(p.ID, p.Birthday)
) FB
WHERE p.ID = FB.ID
With CTE1 (ID, Birthday) AS
(
SELECT * FROM FuncBirthdays(p.ID, p.Birthday)
)
SELECT *
FROM CTE1, Person p
WHERE p.ID = CTE1.ID
I know the title is confusing but its the best I could explain it. Basically im developing a cinema listings website for a company which owns two cinemas. So I have a database which has the two tables "Films" and "Listings" with data for both cinemas in them.
I'm trying to select all films and their data for one cinema if the films name shows up in the listings (since the two cinemas share all films but in the table but the may not have the same films showing)
Here is what i have come up with but I run into a problem as when the "SELECT DISTINCT" returns more than one result it obviously cant be matched with the FilmName on tbl Films.
How can i check this value for all FilmNames on tblFilms?
SELECT *
FROM tblFilms
WHERE FilmName = (SELECT DISTINCT FilmName FROM tblListings WHERE Cimema = 1)
use IN if the subquery return multiple values,
SELECT *
FROM tblFILMS
WHERE FilmName IN (SELECT DISTINCT FilmName FROM tblListings WHERE Cimema = 1)
Another way to solve thius is by using JOIN (which I recommend)
SELECT DISTINCT a.*
FROM tblFILMS a
INNER JOIN tblListings b
ON a.FilmName = b.FilmName AND
b.Cimema = 1
for faster query execution, add an INDEX on FilmName on both tables.
If you have your schemas for the tables, that would help.
That said, I believe what you want to look at is the JOIN keyword. (inner/outer/left/etc). That's exactly what JOIN is meant to do (ie your title).
i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.