Abhishek
Abhishek

Reputation: 31

Fetch data from two table

I am using rails3. I have two table user_shift and other is shift table . I want current shift of the particular user. user_shift table field are

`id`, 
`from`, 
`to`, 
`shift_id`, 
`user_id`, 
`created_at`, 
`updated_at` 

and in shift table fields are

`id`, 
`start_time`, 
`end_time`, 
description`, 
`created_at`, 
`updated_at` . 

Upvotes: 0

Views: 41

Answers (2)

Meherzad
Meherzad

Reputation: 8553

Try this query

SELECT us.id, s.start_time, s.end_time 
FROM user_shift us, shift s
WHERE us.shift_id = s.id AND us.id=?;

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

MySQL joins are the best solution for this.

Example

SELECT * FROM shift LEFT JOIN user_shift ON shift.id = user_shift.shift_id

Shift with user

SELECT * FROM shift LEFT JOIN user_shift ON shift.id = user_shift.shift_id WHERE user_shift.user_id = LOGGEDINUSERID

Upvotes: 3

Related Questions