How to show some columns of 3 tables in SQL? - sql

I have 3 tables: Prestam, Book, User.
Table Prestam
Id_Prestam
Date_Prest
Date_Dev
Id_Book 'link
Id_User 'link
Table Book
Id_Book
Title
Table User
Id_User
Name
Last Name
Here is my SQL code, although there is an error in the syntax, specifically in:
, Pr.Id_User = User.Id_User
SELECT
Pr.Id_Prestam, Pr.Date_Prest, Pr.Date_Dev,
Lbr.Title,
Usr.Name, Usr.Last Name
FROM
Prestam Pr, Book Lbr, User Usr
WHERE
Pr.Id_Book = Lbr.Id_Book, Pr.Id_User = Usr.Id_User
The goal is to show me the following:
Id_Prestam
Date_Prest
Date_Dev
Title
Name
Last Name
Is there any way to make this possible?
I had heard about creating views but, I do not know how to handle it
CREATE VIEW List AS

To resolve that particular syntax error, since you have given the User table the alias of Usr you need to change this:
, Pr.Id_User = User.Id_User
To this:
, Pr.Id_User = Usr.Id_User
Then you'll need to take a look at using the JOIN statement to join tables together. The WHERE statement doesn't allow you to join tables together.

There is a typo in your SQL code.
In this part of the query, you are referring to the User table alias :
, Pr.Id_User = User.Id_User
But that alias does not exist, it is called Usr instead.
Bottom line, you should be using JOINs instead of stuffing your relationships in the WHERE clause.
Here is a new query :
SELECT
Pr.Id_Prestam,
Pr.Date_Prest,
Pr.Date_Dev,
Lbr.Title,
Usr.Name,
Usr.Last Name
FROM Prestam Pr
INNER JOIN Book Lbr on Pr.Id_Book = Lbr.Id_Book
INNER JOIn User Usr on Pr.Id_User = Usr.Id_User

Looks like you can do some sort of join on the tables.
i.e.
select *
from Prestam P
inner join Book B on P.ID_Book = B.ID_Book
inner join User U on P.ID_User = U.ID_User

Join Prestam with the other 2 tables:
SELECT
Prestam.Id_Prestam, Prestam.Date_Prest, Prestam.Date_Dev,
Book.Title,
User.Name, User.`Last Name`
FROM
(Prestam INNER JOIN Book ON Prestam.Id_Book = Book.Id_Book)
INNER JOIN User
ON Prestam.Id_User = User.Id_User;

Solution --> change coma to AND
SELECT
Pr.Id_Prestam, Pr.Date_Prest, Pr.Date_Dev,
Lbr.Title,
Usr.Name, Usr.Last Name
FROM
Prestam Pr, Book Lbr, User Usr
WHERE
Pr.Id_Book = Lbr.Id_Book AND Pr.Id_User = Usr.Id_User

Related

How to join multiple tables in a view

how to create view of this query anyone help me please, i want create view of this but its show me error
Msg 4506, Level 16, State 1, Procedure ordersview, Line 3 Column names
in each view or function must be unique. Column name 'ID' in view or
function 'ordersview' is specified more than once.
CREATE VIEW ordersview
AS
Select * from UserClaimData cd
Inner join UserClaimDeductions ud on
ud.CLAIMID = cd.ID
Inner join UserClaimApproval ua on
ua.CLAIMID = cd.ID
inner join ClaimDataBreakdown cb on
cb.CLAIMID = cd.ID
inner join AppExpenseTypes ae on
ae.ID = cb.EXPENSETYPE
inner join AppNOWTypes an on
ae.ID = an.EXPENSETYPEID
inner join AppAreas aa on
aa.ID = cb.AREAID
inner join AppZones az on
cb.ZONEID = az.ID
inner join AppRegions ar on
ar.ID = cb.REGIONID
The answer to the question you've asked is to specifically reference elements from each table; for example:
CREATE VIEW ordersview
AS
Select cd.ID AS ID1, ua.ID as ID2, etc... from UserClaimData cd
Inner join UserClaimDeductions ud on
ud.CLAIMID = cd.ID
Inner join UserClaimApproval ua on
ua.CLAIMID = cd.ID
inner join ClaimDataBreakdown cb on
cb.CLAIMID = cd.ID
inner join AppExpenseTypes ae on
ae.ID = cb.EXPENSETYPE
inner join AppNOWTypes an on
ae.ID = an.EXPENSETYPEID
inner join AppAreas aa on
aa.ID = cb.AREAID
inner join AppZones az on
cb.ZONEID = az.ID
inner join AppRegions ar on
ar.ID = cb.REGIONID
I would, however, suggest that you don't put such a complex join inside a view. Consider the columns you want, and perhaps think about a stored procedure, or a table value function.
What part of the error message do you not understand?
You have select *, which brings together all columns from all tables. Just based on the join conditions, it is clear that most tables have an ID column, so there are multiple columns called ID. CLAIMID also seems quite popular.
In general, using select * is discouraged. However, it should not be used for views. A view should state the columns that it contains:
select cd.Id, . . .
Your view has more than one column with the same name, and this is causing the error.
AppRegions has a column called ID
AppAreas has a column called ID
UserClaimData has a column called ID
Change the Select * to specify the columns of the tables you want to have on the View table, and put a particularry name, or just don't put them.
You need to change the name of some columns in the view. Use AppAreas.ID as aaID
A view is like a virtual table, So you can't have the same name for 2(or more) columns.
So to avoid this, in your select Query instead of * provide the column names, and if there are 2 columns with the same name and you need them both in the view, give them different alias names.
Suppose you have ColumnA in TableA and TableB and you want them both in the view. Create view like this
CREATE VIEW vm_Sample
SELECT
A.COLUMNA COLUMNA_1,
B.COLUMNA COLUMNA_2
FROM TABLEA A INNER JOIN TABLE B
ON A.ID = B.ID

SQL Where on different table

SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id
Teacher_id being the session id,
I want to see all the students that have the same mentor.
Right now if I run this I just see all of the students twice, maybe one of you knows why?
My db scheme
You are not specifying on which columns you want to do the join, so you're getting a cross reference where all records are joined to all records.
You should do something like (not sure about your column names):
SELECT * FROM student_mentor sm INNER JOIN users u
ON sm.student_id = u.user_id
WHERE sm.teacher_id = $teacher_id

sql inner join in many to many relationship

i have a table users(user_id, username, password etc),
a table courses(course_id, coursename, etc)
and a table student_favouriteCourses(user_id, course_id) //both as primary key
and i would like if i know the id of the user, to search for all the courses that he has as favourites. can u help me with the join query please?
Maybe something like this:
SELECT
*
FROM
student_FavouriteCourse fav
JOIN student stu ON ( fav.user_id = stu.user_id)
JOIN courses crs ON ( fav.course_id = crs.course_id)
WHERE
fav.user_id = <yourvalue>
As an explanation:
1) this will return the student_FavouriteCourse (using "fav" as an alias) records you want
SELECT
*
FROM
student_FavouriteCourse fav
WHERE
fav.user_id = <yourvalue>
2) you then JOIN the student details (using "stu" as an alias)
JOIN student stu ON ( fav.user_id = stu.user_id)
3) you then JOIN the course details (using "crs" as an alias)
JOIN courses crs ON ( fav.course_id = crs.course_id)
Can I suggest that if you are new to JOINs and SQL, you run this query at each of steps above and then you will see how the join is working and how the resulting dataset is constructed.
EDIT: Read your post again and I see you don't actually need the student join, just the courses. Therefore:
SELECT
*
FROM
student_FavouriteCourse fav
JOIN courses crs ON ( fav.course_id = crs.course_id)
WHERE
fav.user_id = <yourvalue>
I think your trying a very basic MySQL query, can you please next time try to put your code in so we can help in your mistakes and you can learn from them?
Anyway the code youre trying to do should be something like this:
select u.id, u.username, c.coursename
from users u
left join student_favouriteCourses f on u.id = f.user_id
left join courses c on f.course_id = c.id
where u.username = '<YourUsername>'
This will return your user join with your favourite courses and then the course.
In case you want to make sure you show at least 1 course (confirm that course exist) inner join will help you:
select u.id, u.username, c.coursename
from users u
inner join student_favouriteCourses f on u.id = f.user_id
inner join courses c on f.course_id = c.id
where u.username = '<YourUsername>'
There are many ways to do those joins depends of your scenario and how you like to code that.
I leave you here a link that explains this much better.

Query from 3 tables

I have 3 tables: TblUsers, tblBridge, and tblAssignments.
The bridge contains the ID of the user and the corresponding AssignmentID for that user.
I'm trying to find the user with a certain assignment #Assignment, but also where tblUser.isAdmin = Yes.
I feel like its possible with Joins, I just can't find a good example on how to get it done.
I'm assuming this is in a stored procedure which receives an #assignment var.
select tu.[Users] from TblUsers tu
join tblBridge tb on tu.id=tb.TblUsersid
join tblAssignments ta on ta.id = tb.tblAssignmentsid
where ta.[Assignment] = #Assignment
and tu.[isAdmin] = 'Yes'
SELECT u.* FROM tblBridge b
JOIN tblUsers u ON u.ID = b.tblUsersID
JOIN tblAssignments a ON a.ID = b.tblAssignmentsID
WHERE u.isAdmin = 'yes' AND a.name = 'searched assignment'
It's MANY-TO-MANY Relation so you need select many-to-many table and join it to assigments and users also put in where condition for admin.
I'm not sure if the names are exact the same as you have in your DB so you need adjust it to yor needs.
I don't know the field that you look for in assigment table so change a.name to your needs.

Access SQL LEFT JOIN not returning results

I have three tables: Comments, Users and CommentsHelpfulness.
Users can submit several comments, which are stored in the Comments table.
CommentsHelpfulness has three columns: UserID, CommentID and a "Helpful" Boolean. Every user can indicate for every comment if they find it useful, which will create an entry in the CommentsHelpfulness table.
I want a query that gives me all Comment IDs, with the name of the user that submitted it and shows whether the currently logged in user found it helpful, did not find it helpful or did not say anything about it. So the ID of a comment the current user did not express his opinion about should still be output, just without the helpful Boolean.
To me that sounds like it should be done like this using a left join:
SELECT Comments.ID, Users.Nom, CommentsHelpfulness.Helpful
FROM (Comments INNER JOIN Users
ON Comments.UserID = Users.ID)
LEFT JOIN CommentsHelpfulness
ON (CommentsHelpfulness.CommentID = Comments.ID
AND (CommentsHelpfulness.UserID = ?))
Unfortunately this does not output Comment IDs without an entry in the CommentsHelpfulness table. Why does this not work? Is it because of Access?
I think the issue is the inner join, not the left join.
Try removing that table:
SELECT c.ID, ch.Helpful
FROM Comments as c LEFT JOIN
CommentsHelpfulness as ch
ON ch.CommentID = c.ID AND
ch.UserID = ?
select c.id, c.Nom, d.Helpful from (
select a.ID, b.Nom from Comments a left join Users b
on a.UserID = b.ID) c left join CommentsHelpfulness d
on d.CommentID = c.ID;