English 中文(简体)
# C# 数据格网视图,多张表格将查询连为一体
原标题:C# datagridview with multiple table join query
  • 时间:2012-05-23 22:10:42
  •  标签:
  • c#
  • mysql

我有一个问题 如何将数据绑在 C# 的数据格上, 当使用 JOIN 查询与 MySql 数据源, 我从第一个表格返回一个独特的记录, 从第二个表格返回 0-2 记录。 我的查询如下:

SELECT t1.*, t2.org_id, t2.org_name 
FROM test.tbl_user_accounts AS t1 
INNER JOIN test.tbl_organizations AS t2 
ON t1.affiliation_one = t2.org_id OR t1.affiliation_two = t2.org_id;

这是一种学校授标, 用户可以与最多两所学校连在一起。 我希望学校可以分开显示, 这样Group_ CoONCAT并不是一个好选择, 如果我在 t1. user_ id 字段上使用 Group by the t1. user_ id field, 我就会失去第二个关联。 如果我不分组, 我最后会为同一个用户设置两行 。

我不是所有熟悉加入的人,所以有可能 另一种类型的加入可以解决这个问题吗?

我使用2010年的视觉C#2010 和我的Sql数据源。

最佳回答

在这里我用来解答这个问题的查询(我想我在另一个堆叠柱上找到的) 。

SELECT test.tbl_user_accounts.*, t2.org_name as affil_one, t3.org_name as affil_two,t4.rank_name as rank_name
FROM test.tbl_user_accounts 
LEFT JOIN (test.tbl_organizations as t2) ON (t2.org_id = test.tbl_user_accounts.affiliation_one)
LEFT JOIN (test.tbl_organizations as t3) ON (t3.org_id = test.tbl_user_accounts.affiliation_two)
LEFT JOIN (test.tbl_ranks as t4) ON (t4.id_rank = test.tbl_user_accounts.user_rank);
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签