【问题标题】:creating a subquery of all the users in a table which has their id in the other tabel创建一个表中所有用户的子查询,其 id 在另一个表中
【发布时间】:2019-05-25 23:43:50
【问题描述】:

我有 2 个表:表 1 和表 2。此表有不同的字段,但是表 1 中的字段“uid”始终与表 2 中的字段“uid”具有相同的值。并非 table1 中的所有 id 在 table2 中都具有相同的值,但 table2 中的所有 id 在 table1 中具有相同的值。我正在尝试对我进行查询,为我预设“uid”在另一个表中具有相同值的所有记录,“uidd”中的 table2,在这种情况下,创建一个包含来自 table1 的人员行的 DataTable。这怎么可能?我试过了:

    public void ChackForActiveUsers()
    {
        string name = Request.QueryString["n"];
        string fileName = "UsersDB.accdb";
        string sql = "";
        sql += "SELECT * FROM table1 WHERE uid = '(SELECT uidd FROM table2)'";
        DataTable dt = MyAdoHelper.ExecuteDataTable(fileName, sql);
        if (dt.Rows.Count != 0)
        {
            output += "name - lastname - id - email - password - gender - age" + "<br/>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                output += "<br/><form id='mform' action='' method='get' ><input type='text' name='fname' value='" + dt.Rows[i][0] + "'/> - <input type='text' name='lastname' value ='" + dt.Rows[i][1] + "'/> -  <input type='text' name='id' value ='" + dt.Rows[i][2] + "'/> -  <input type='text' name='email' value='" + dt.Rows[i][3] + "'/> -  <input type='text' name='password' value='" + dt.Rows[i][4] + "'/> -  <input type='text' name='gender' value='" + dt.Rows[i][5] + "'/> -  <input type='text' name='age' value='" + dt.Rows[i][6] + "'/> - " + "<input type = 'text' name = 'isadmin' value = '" + dt.Rows[i][7] + "' /> <input type='submit' name='sumbm' value='update' /><input type='submit' name='sumbmm' value='ban' />";

            }
        }
        else
        {
            output = "not found";
        }
    }

到目前为止,我得到的输出是“未找到”,但在数据库中有行可以意识到这种情况。

【问题讨论】:

    标签: c# sql


    【解决方案1】:

    听起来您想使用EXISTS 和相关子查询来搜索另一个表中的 ID。

    SELECT *
           FROM table1 t1
           WHERE EXISTS (SELECT *
                                FROM table2 t2
                                WHERE t2.uidd = t1.uid);
    

    另一个更接近您的语法的选项是使用IN。但是当table2 很大时,这可能会更慢。

    SELECT *
           FROM table1 t1
           WHERE t1.uid IN (SELECT t2.uuid
                                   FROM table2 t2);
    

    【讨论】:

    • 什么是 t1?和t2?
    • @clearwebcom: Aliases.
    • 没有为一个或多个必需参数指定值
    • 这是错误
    • @clearwebcom:好的,但这不太可能来自这些查询。
    【解决方案2】:

    你的这个查询在大多数数据库中都不起作用,因为表通常存储超过 1 行:

    WHERE uid = '(SELECT uidd FROM table2)'
    

    WHERE 之后的 select 必须始终返回 1 行,否则会抛出错误。如果您希望此条件能够处理更多uidd,则需要将= 替换为IN

    【讨论】:

    • 仍然没有工作。输出=未找到
    • 我想你应该把这个“SELECT * FROM table1 WHERE uid = '(SELECT uidd FROM table2)'”改成这个“SELECT * FROM table1 WHERE uid = (SELECT uidd FROM table2)”。 SQL Server 将 '(SELECT uidd FROM table2)' 读取为字符串而不是选择。
    • 错误:这个子查询最多可以返回一条记录。
    • 在我看来,还没有阅读我的帖子... WHERE uid in (SELECT uidd FROM table2)
    【解决方案3】:

    尝试将 '=' 更改为 'in':

    SELECT * FROM table1 WHERE table1.uid in (SELECT table2.uidd FROM table2)
    

    【讨论】:

    • 输出仍未找到
    猜你喜欢
    • 2011-01-02
    • 2015-07-02
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2016-07-26
    相关资源
    最近更新 更多