简体   繁体   English

MySQL Join语句从两个表中获取数据到datagridview

[英]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. 用户包含我的所有用户信息,“ id”列是主要且唯一的。 Holidays contains a column called 'userid', which corresponds to the id in the user table. 假期包含一个名为“ userid”的列,它对应于用户表中的id。

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. 我正在努力使join语句正常工作...我想要的是select语句的结果,该结果是为我提供友好名称(用户表中的列“ fname”),而不是为我提供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. 但是我没有得到正确的结果-SQL执行没有错误,但是我的DGV充满了很多错误的结果。

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: 您需要从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 通过别名( FROM users u ),您可以通过u.fname从users表中选择列

First try to right join to the User table. 首先尝试右连接到User表。 If you just want the fname then select the column name in the SELECT query, as SELECT * takes more time then SELECT column name . 如果只需要fname则在SELECT查询中选择column name ,因为SELECT *SELECT column name花费更多的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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