Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining tables in MySQL and requesting data from second table only

Tags:

mysql

I'm trying to join 2 tables where I need to show only 3 columns from the second one where another column is used as a comparison.

For example:

Table one is called employee: it has a column called user_id and some other columns

Table two is called people: it has a column called user_id which included some of the employees user_ids

The columns I want to select are all from table people! (firstname, lastname, email)

I tried the following but something going wrong:

SELECT userid, firstname, lastname, email 
FROM people 
JOIN employee 
WHERE people.userid = employee.userid;

I'm not sure what am I doing wrong, could you please help me correct it?

like image 317
Sam Edward Avatar asked Dec 25 '17 21:12

Sam Edward


People also ask

How fetch data from two tables in join MySQL?

Ans: Joining two tables in SQL can be done in four major ways: Inner Join (returns rows with matching columns), Left Join (ALL records in the left table and matching records in the right table), Right Join (ALL records in the right table and matching records in the left table), and Union (removes duplicates).

How can I extract data from two tables?

(INNER) JOIN : Returns records that have matching values in both tables. LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table.

How do I SELECT data from two tables in SQL?

In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables.


3 Answers

You can try this query:

SELECT 
     p.userid, 
     p.firstname, 
     p.lastname, 
     p.email 
FROM 
     people as p, 
     employee as emp 
WHERE 
     p.userid = emp.userid
like image 141
DirWolf Avatar answered Nov 02 '22 06:11

DirWolf


Looking at your script, it looks like you'll run into ambiguous columns in at least your userid. You want to explicitly tell SQL where the column comes from like in your WHERE clause if there are columns sharing the same name between the two tables.

SELECT
  userid, -- AMBIGUOUS 
  firstname,
  lastname, 
  email 
  FROM people
    JOIN employee 
  WHERE people.userid = employee.userid;

Example solution:

SELECT
  people.userid,
  people.firstname,
  people.lastname,
  people.email
  FROM people
    JOIN employee 
  WHERE people.userid = employee.userid;
like image 38
Dan Avatar answered Nov 02 '22 06:11

Dan


For this issue you can use this query

let suppose that I have a users table where a user have zero to one profile picture

I need the user (Name,LastName,BirthDate) for users who have no profile picture I can use this query

select * 
from user c
where  NOT EXISTS (
    select 1 
    from photo p
    where p.id = c.photo_id
)

in this where you can use any field between this two table

removing the not will result on the users who have a profile picture

hope this help you

you can search for SEMI JOIN and ANTI JOIN for more informations

like image 26
Mohammed Housseyn Taleb Avatar answered Nov 02 '22 07:11

Mohammed Housseyn Taleb