• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Run mysql query from two tables

I am trying to create a report but having some issues, I need to pull a from a department column in one table and then take those results and use the login column from that to search in the second table. Can anyone help me with this? Thanks
  • 2
1 Solution
Giovanni HewardCommented:
You'll need to use a JOIN statement... example:

select * from `db`.`login_table` l
join `db`.`department_table` d on d.`id`=l.`dept_id`;

Open in new window

maximus81Author Commented:
So what would that look like with this information.

Database ATS

Table - atsusers - Colunms - department, login
Table - atsassets - Colunms - asset, asignedto

I want to display the asset name from atsassets and would like to search using the department, this would give me all the logins from atsusers table, next I want to search the atsassets asignedto colunm using the logins from atsusers. This is so confusing.
Kevin CrossChief Technology OfficerCommented:
As stated above, it sounds like you want a JOIN.

SELECT a.asset, a.asignedto
FROM atsassets a
JOIN atsusers u ON u.login = a.asignedto
WHERE u.department = 'departement you want to search'

maximus81Author Commented:
Thank you so much, you have saved me hours of trying to figure this out.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now