SQL statement from 2 tables - mysql

I am currently trying to run a sql statement to pull data from 2 tables I was wondering if someone could help me with my code. I would like to pull Project.Project_ID, Project.StartDate, Project.Users where Project_ID = 1 in both tables.
SELECT Project.Project_ID, Project.StartDate, Project.Users
FROM Project
INNER JOIN UserList ON Project.Project_ID=UserList.Project_ID
WHERE Project.Project_ID = '1';

I am not exactly sure what is your problem statement. If you can provide more details (DB Structure) that would be really helpful.
There could be more that one scenario you want to solve based on your data structure.
The only thing I see here is instead of Project.Users you should have UserList. Users. I assume User list tables having all the users details and it's not a pivot table. If it is then you need another join with User table.
SELECT
Project.Project_ID, Project.StartDate, UserList.Users
FROM
Project
INNER JOIN UserList ON Project.Project_ID=UserList.Project_ID
WHERE
Project.Project_ID = '1';
Please share your Database Structure.

With your model a user can only be linked to one project.
Below is with an joining table UserProject.
This way n users can take part to n projects. (n to n relation)
SELECT
p.id AS projectId,
p.startDate,
u.id AS userId,
u.name,
[...]
FROM
Project AS p,
UserProject AS up,
User AS u
WHERE
p.id = up.projectId
AND u.id = up.userId
AND p.id = 1;
And here is with a 1 to 1 relation
[...]
FROM
Project AS p,
User AS u
WHERE
p.id = u.projectId
AND p.id = 1;

Related

Join two tables query

I have two tables users and linkage. I am creating a link between one user to another. Means user A will be linked to user B and vise versa.
Now I want to get the details of linked users for a particular entered user_id. Means If user A is finding his linked Id's then the details of linked id's should be seen.
Linkage has three columns its id, user_id and linked_contact_id.
Users has columns as user_id, user_name,pass etc..
I tried one join but I only get the linked Id's from this not the details of id's.
SELECT * FROM Users INNER JOIN linkage ON linkage.user_id = Users.user_id WHERE linkage.linked_contact_id = 1
output
user_id linked_contact_id
1 4
1 1
1 5
How can I get this?Please help.. Thank you...
You will need to join on the users table a second time:
SELECT u.*, u2.* FROM Users u
INNER JOIN linkage l ON l.user_id = u.user_id
INNER JOIN Users u2 ON l.linked_contact_id = u2.user_id
WHERE l.linked_contact_id = 1
Please note that as you are selecting the same columns twice (in u.*, u2.*), you will probably have to list out each field with an alias to distinguish between them.
Something wrong on your ratio. But it's okay.
Try LEFT JOIN or RIGHT JOIN
SELECT * users
LEFT JOIN linkage
ON linkage.user_id = users.user_id
WHERE linkage.linked_contact_id = 1
Get Users information
SELECT users.*,
linkage.*,
contact.user_id as contact_user_id,
contact.user_name AS contact_user_name
FROM users
INNER JOIN linkage ON linkage.user_id = users.user_id
INNER JOIN users AS contact ON linkage.linked_contact_id = contact.user_id
WHERE linkage.linked_contact_id = 1
Edit
Here Screenshot of Query Output

Select only some entries based on what's in another table

I'm trying to grab all of the offers provided to a user that are not already queued up in their projects. So, I'm grabbing offers from the OfferSuggestionHeader table based on my user's ID but also try to make sure it doesn't grab anything that the user has already added to their projects (stored in the Projects table)
I've got the following query:
SELECT DISTINCT ofh.OfferID, ofh.OfferTitle, ofh.OfferVendor, ofh.Savings,ofh.SavingsPercent
FROM OfferSuggestionHeader ofh
LEFT JOIN OfferSuggestionDetail osd
ON ofh.OfferID = osd.OfferID
LEFT JOIN Facilities f
ON osd.FacilityID = f.id
LEFT JOIN UserFacility uf
ON f.id = uf.fid
LEFT JOIN Users u
ON uf.uid = u.uid
LEFT JOIN Projects p
ON p.uid = u.uid
WHERE p.uid = '1'
AND ofh.OfferID <> ANY (SELECT offer_id FROM Projects WHERE uid = '1')
It pulls up all of the offers. If I take away ANY then I get an error saying that the subquery returns too many results.
There are 6 offers. Three are queued up by user 1. I shouldn't see offers 1, 4, or 5.
Thanks for any pointers and help.
As per my comment explanation, why not try this? I am not sure why you need a subquery when you already have PROJECT table JOINED in your main query..
WHERE p.uid = '1'
AND ofh.OfferID NOT IN (1, 4, 5);

Selecting all projects and tasks for a specific userID

I'm trying to select all Projects and Tasks from my database when given a specific userID.
The tables are as follows:
Users: UserID
Projects: ProjectID, UserID
Tasks: TaskID, ProjectID
I've tried things with nested queries, table joins and such but I'm no SQL expert by far and I'm struggling to get what I need. I can get all Tasks from a specific userID but that uses an Inner Join on the ProjectID which means that it doesn't return Projects that have 0 Tasks as Projects with no Tasks have nothing to be joined with.
You need to use LEFT JOIN to return the data:
select *
from users u
left join projects p
on u.userid = p.userid
left join tasks t
on p.projectid = t.projectid
where u.userid = ?
See SQL Fiddle with Demo
This will return all users regardless of whether they have records in the projects or tasks table.

SQL Select -> product -> comments -> user

thanks for yout time helping on this ;)
I'm new to SQL and wish to solve somethign in just one query and i dont know how to do it.-
Basically I've a table of products, a table of users and a table of comments, linked by products.id -> comments.pid and user.id -> comments.uid ,
i wish to know what is the best practice to create just 1 query and get all products with child comments, including username.
Learn about SQL joins:
SELECT *
FROM products
LEFT JOIN comments ON products.id = comments.pid
LEFT JOIN user ON user.id = comments.uid
learning joins is the best way to solve such a problem
Explanation
what we are trying here to do is create a virtual table which fetches records based on some relations.
I have created three tables here
product
user
comments
and there is a relation set between the
product and the comments table - using product.id and comments.prod_id
user and the comments table - using user.id and comments.user_id
Now you can use join to filter the results
SELECT product.id,user.name,comments.DATA
FROM comments
LEFT JOIN product ON comments.prod_id = product.id
INNER JOIN USER ON comments.user_id = USER.id;
or if you don't want to use join you can simply create a relation
but please do note that using joins is the best possible way to do it
SELECT product.id,USER.name,comments.DATA
FROM product,USER,comments
WHERE product.id = comments.prod_id AND USER.id = comments.user_id;
you can check the results here
SqlFiddle
Istead of using JOIN you can try this,
select p.id,u.name,c.data
from products as p,user as u,comment as c
where p.id = c.pid and u.id = c.uid;

MySQL SQL command to query from one table with three same columns to another table

Sorry but I am not sure how to ask this question but I am working on a help desk application where I have tickets being created in one table. I also have another table that stores the users. My problem is with the tickets table, I have listed the user that created the ticket, the tech who will solve the ticket and a user that over sees the ticket. All three users reference the users table. So how do I can I query the tickets table and get all three users that reference the same table storing the users?
Table1: Tickets
1) Ticketnumber
2) EnteredBy User 100
3) Issue
4) FixedBy User 102
5) FixedByNotes
6) ResponsilbeUser User 103
Table2: Users
1) UserID
2) UserName
What I can do now is something like this:
Select Ticketnumber, EnteredBy, Issue, UserName FROM Tickets INNER JOIN Users
ON Tickets.EnteredBy = Users.UserID
Thanks Steve
You can extend current query to somewhat as follows:
Select Ticketnumber, Issue, Reporter.UserName, Developer.UserName, Manager.UserName FROM Tickets
INNER JOIN Users AS Reporter ON Tickets.EnteredBy = Reporter.UserID
INNER JOIN Users AS Developer ON Tickets.FixedBy = Developer.UserID
INNER JOIN Users AS Manager ON Tickets.ResponsibleUser = Manager.UserID
You need alias for joint tables if you want to get all names:
Select Ticketnumber, Issue, Informers.UserName, Fixers.UserName, Supervisors.UserName FROM Tickets
INNER JOIN Users Informers ON Tickets.EnteredBy = Users.UserID
INNER JOIN Users Fixers ON Tickets.FixedBy = Users.UserID
INNER JOIN Users Supervisors ON Ticket.ResponsibleUser = Users.UserID
WHERE...
Sorry as i am not able to understand your words, but if I assumed your need correctly.. just for a try this could help you..
if you need either of them i.e. all users who has either entered or fixed or saw an issue you can find by..
Select t.Ticketnumber, t.EnteredBy, t.Issue, u.UserID ,u.UserName FROM Tickets t
INNER JOIN Users u ON t.EnteredBy = u.UserID or t.FixedBy = u.UserID
or t.ResponsibleUser = u.UserID;
And If you need all users who has entered, fixed and saw an issue you can find by..
Select t.Ticketnumber, t.EnteredBy, t.Issue, u.UserID ,u.UserName FROM Tickets t
INNER JOIN Users u ON t.EnteredBy = u.UserID and t.FixedBy = u.UserID
and t.ResponsibleUser = u.UserID;