从一个表中选择字段并在另一个表中匹配并返回值[关闭]

i have 2 tables

categories

categories (cid, year_id, category_title)

users

users (uid, year_id, name, rollno, password)

when user login with his rollno and password i want to show his category by selecting year_id from 'user' and matching for that year_id in 'categories'

i'm trying

SELECT * FROM categories WHERE (select year_id='$year' from users)

Use a simple join

SELECT c.* FROM categories c
JOIN users u ON(c.year_id =u.year_id)
WHERE u.year_id='$year' 

Edit

SELECT c.*,u.year_id user_year FROM categories c
JOIN users u ON(c.year_id =u.year_id)
WHERE  u.uid='$user_id'

If the year_id is the same for both, and you know the year_id that was returned from the users table, just use that value in a select on the categories table.

SELECT *
FROM `categories`
WHERE `year_id` = '[year_id from users data]'

No join needed.

UPDATE:

To validate the user login:

SELECT *
FROM `users`
WHERE `rollno` = '[users rollno]'

Then compare the password returned from that query to the password provided by the user, and if they match, you know the user is valid, and can use the year_id that was also returned to grab the categories.