简体   繁体   中英

MySQL Join statement to get data from two tables into a datagridview

I have two tables that I'm trying to join, 'holidays' and 'users'.

Users contains all my user info, the the column 'id' being primary and unique. Holidays contains a column called 'userid', which corresponds to the id in the user table.

I'm struggling to get the join statement to work... what I'm looking for is the result of the select statement to give me the friendlyname (column 'fname' in user table) instead of giving me the value of userid.

Here's what I'm trying...

 SELECT * FROM holidays JOIN users on users.id=holidays.userid WHERE holidays.status = 0

But i'm not getting a correct result - SQL executes without error, but my DGV is filled with tons of erroneous results.

Apologies If I have not used the correct terminology or whatever. I'm new to the concept of joins.

Here is hopefully a better explanation of what I am after...

在此处输入图片说明

Thanks in advance.

You need to select the specific values you want from every table in the JOIN:

SELECT u.fname
FROM holidays h
JOIN users u
ON u.id = h.userid
WHERE h.status = 0

by the alias ( FROM users u ) you can select column from users table by u.fname

First try to right join to the User table. If you just want the fname then select the column name in the SELECT query, as SELECT * takes more time then SELECT column name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM