mysql - C#/SQL 连接问题

标签 mysql sql

我有两张 table 。

Table 1: supportstatus

clientid
company
status

Table 2: client
clientid
company
status
state

我还使用一个包含 2 列的表格来分隔 GridView 结果。我目前正在尝试进行两个查询,这些查询将返回一个表列中具有禁用状态的客户端以及另一表列中的相应状态。任何有关解释的帮助将不胜感激。

protected void queryClientCompanyName()
{
    string MyConString = "Driver={MySQL ODBC 5.1 Driver};Server=**;Database=support;User=**; Password=**;Option=3;";
    string queryString = "SELECT company FROM supportstatus WHERE status = 'disabled' Order By company";
    OdbcConnection conn = new OdbcConnection(MyConString);
    conn.Open();
    OdbcCommand command = new OdbcCommand(queryString, conn);
    OdbcDataReader reader = command.ExecuteReader();
    populateGrid(reader, GridView1);
    conn.Close();
    RecordCountLabel.Text = GridView1.Rows.Count.ToString();
}

protected void queryClientCompanyState()
{
    string MyConString = "Driver={MySQL ODBC 5.1 Driver};Server=**;Database=support;User=**; Password=**;Option=3;";
    string queryString = "SELECT cl.state FROM clients cl, supportstatus su WHERE cl.clientid = su.clientid  AND su.status='disabled' Order By cl.company";
    OdbcConnection conn = new OdbcConnection(MyConString);
    conn.Open();
    OdbcCommand command = new OdbcCommand(queryString, conn);
    OdbcDataReader reader = command.ExecuteReader();
    populateGrid(reader, GridView2);
    conn.Close();
}

最佳答案

您应该只需要以下查询来替换您的两个查询:

SELECT su.Company
   , cl.state
FROM clients cl
INNER JOIN supportstatus su ON cl.clientId = su.clientId
WHERE su.status = 'disabled'
Order By cl.company

您希望一个查询能够返回这两个值,以便您知道它们是相关的。正因为如此,您希望将表连接在一起。我刚刚进行了快速谷歌搜索,然后 W3schools有基本的加入信息,techrepublic也有这些应该可以帮助您了解查询数据库的基本概念。

关于mysql - C#/SQL 连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8043625/

相关文章:

mysql - 多步骤数据库查找 Kettle/Pentaho

MySql存储过程调用错误

sql - 在 (DB2) SQL 中选择上个月的第一天

sql - MySQL 中的 !col 和 col=false 有什么区别?

mysql - 自动为字符生成唯一的 id mysql

MySQL:使用if else在select语句中进行子查询

mysql - MySQL中如何进行双连接

php - 查询应返回整数和字符串 CI

javascript - 当尝试将上传文件的路径插入 MySQL 时,会创建重复文件

sql - 数据库设计 : third table that relates to both parent and child tables