select tab1.tabl_name 
from 
tab1,tab2,tab3,tab4
where tab4.tab4_id=tab3.tab4_id 
and tab3.tab3_id=tab2.tab3_id 
and tab2.tab2_id=tab1.tab2_id 
and tab4.my_id=3
绕晕了。

解决方案 »

  1.   

    select tab1.tabl_name from tab1 a ,tab2 b ,tab3 c ,tab4 d 
    where d.tab4_id=c.tab4_id and c.tab3_id=b.tab3_id and b.tab2_id=a.tab2_id 
    and c.my_id=3
      

  2.   

    select a.tabl_name
    from tab1 a,tab2 b,tab3 c,tab4 d
    where d.tabl4_id=c.tabl4_id
    and c.tabl3_id=b.tabl3_id
    and b.tabl2_id=a.tabl2_id
    and d.my_id=3
      

  3.   

    select a.tabl_name 
    from tab1 a
    join tab2 b on a.tabl2_id = b.tab2_id
    join tab3 c on b.tabl3_id = c.tab3_id
    join tab4 d on c.tabl4_id = d.tab4_id
    where d.my_id = 3
      

  4.   

    这里才比较清楚,在access写的不明白
      

  5.   

    select a.tabl_name 
    from tab1 a right join tabl2 b on a.tabl2_id=b.tabl2_id
    right join tabl3 c on b.tabl3_id=c.tabl3_id
    right join tabl4 d on c.tabl4_id=d.tabl4_id
    where my_id=3
      

  6.   

    不好意思,问一句上面各位老大给的东东里面的a b c d 分别是什么?我怎么不懂啊
      

  7.   

    我的最后的c.my_id=3应为d.my_id=3,楼主实在对不起
      

  8.   

    看见了今天最强SQL绕口令阵容,可以安心睡觉了。
      

  9.   

    select tab1.tabl_name 
    from 
    tab1,tab2,tab3,tab4
    where tab4.tab4_id=tab3.tab4_id 
    and tab3.tab3_id=tab2.tab3_id 
    and tab2.tab2_id=tab1.tab2_id 
    and tab4.my_id=3

    select a.tabl_name 
    from tab1 a right join tabl2 b on a.tabl2_id=b.tabl2_id
    right join tabl3 c on b.tabl3_id=c.tabl3_id
    right join tabl4 d on c.tabl4_id=d.tabl4_id
    where my_id=3都为等连接,速度慢,如果只需要name,可以用子查询。如:
    select a.tabl_name 
    from tab1 a 
    where a.tab1_id in (select tab2_id from tab2 where tab2id in (select tab3_id from tab3 where tab3id in (select tab4_id from tab4 where my_id = 3 )))
      

  10.   

    我用的是access,没有办法到底是不是我得程序错了。。现在不抱错了,可是结果却出不来我的程序这样写的

    sql="select a.tabl_name
    from tab1 a,tab2 b,tab3 c,tab4 d
    where d.tabl4_id=c.tabl4_id
    and c.tabl3_id=b.tabl3_id
    and b.tabl2_id=a.tabl2_id
    and d.my_id=3"
    _RecordsetPtr m_pRecordset;
    m_pRecordset.CreateInstance("ADODB.Recordset");
    _variant_t RecordsAffected;
    m_pRecordset =m_pConnection->Execute((_bstr_t)sqlstr,&RecordsAffected,adCmdText);
    if(VARIANT_FALSE == m_pRecordset->adoEOF)  
    {
    int i=0;
    _variant_t vTemp;
    CString tabl_name;
    while(!m_pRecordset->adoEOF)
    {    
    vTemp=m_pRecordset->GetCollect("tabl_name");
    tabl_name=(char *)(_bstr_t)vTemp;

    i++;
    m_pRecordset->MoveNext();
    }
    vTemp.Clear();
    }谁能说说什么地方错了
      

  11.   

    select tab1_name from(
    select tab1_name,tabl3_id from tab1 a
    join tab2 b on a.tabl2_id=b.tabl2_id
    ) a join (
    select tab3_id from tab3 a
    join tab4 b on a.tab4_id=b.tab4_id
    where b.my_id=3
    ) b on a.tabl3_id=b.tabl3_id
      

  12.   

    'ACCESS中应该这样写:SELECT a.tabl_name
    FROM ((tab1 AS a INNER JOIN tab2 AS b ON a.tabl2_id = b.tab2_id) INNER JOIN tab3 ON b.tabl3_id = tab3.tab3_id) INNER JOIN tab4 AS d ON tab3.tab4_id = d.tab4_id
    where d.my_id=1
      

  13.   

    上面各位得方法我都试过了。没有可以得郁闷。是不是因为我用的是access的缘故
      

  14.   

    外联接。外联接可以是左向外联接、右向外联接或完整外部联接。 
    在 FROM 子句中指定外联接时,可以由下列几组关键字中的一组指定:LEFT JOIN 或 LEFT OUTER JOIN。 
    左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。RIGHT JOIN 或 RIGHT OUTER JOIN。 
    右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。FULL JOIN 或 FULL OUTER JOIN。 
    完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。仅当至少有一个同属于两表的行符合联接条件时,内联接才返回行。内联接消除与另一个表中的任何行不匹配的行。而外联接会返回 FROM 子句中提到的至少一个表或视图的所有行,只要这些行符合任何 WHERE 或 HAVING 搜索条件。将检索通过左向外联接引用的左表的所有行,以及通过右向外联接引用的右表的所有行。完整外部联接中两个表的所有行都将返回。Microsoft® SQL Server™ 2000 对在 FROM 子句中指定的外联接使用以下 SQL-92 关键字: LEFT OUTER JOIN 或 LEFT JOIN
    RIGHT OUTER JOIN 或 RIGHT JOIN
    FULL OUTER JOIN 或 FULL JOIN 
    SQL Server 支持 SQL-92 外联接语法,以及在 WHERE 子句中使用 *= 和 =* 运算符指定外联接的旧式语法。由于 SQL-92 语法不容易产生歧义,而旧式 Transact-SQL 外联接有时会产生歧义,因此建议使用 SQL-92 语法。使用左向外联接
    假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。若要在结果中包括所有的作者,而不管出版商是否住在同一个城市,请使用 SQL-92 左向外联接。下面是 Transact-SQL 左向外联接的查询和结果:USE pubs
    SELECT a.au_fname, a.au_lname, p.pub_name
    FROM authors a LEFT OUTER JOIN publishers p
    ON a.city = p.city
    ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC下面是结果集:au_fname au_lname pub_name 
    -------------------- ------------------------------ ----------------- 
    Reginald Blotchet-Halls NULL
    Michel DeFrance NULL
    Innes del Castillo NULL
    Ann Dull NULL
    Marjorie Green NULL
    Morningstar Greene NULL
    Burt Gringlesby NULL
    Sheryl Hunter NULL
    Livia Karsen NULL
    Charlene Locksley NULL
    Stearns MacFeather NULL
    Heather McBadden NULL
    Michael O'Leary NULL
    Sylvia Panteley NULL
    Albert Ringer NULL
    Anne Ringer NULL
    Meander Smith NULL
    Dean Straight NULL
    Dirk Stringer NULL
    Johnson White NULL
    Akiko Yokomoto NULL
    Abraham Bennet Algodata Infosystems
    Cheryl Carson Algodata Infosystems(23 row(s) affected)不管是否与 publishers 表中的 city 列匹配,LEFT OUTER JOIN 均会在结果中包含 authors 表的所有行。注意:结果中所列的大多数作者都没有相匹配的数据,因此,这些行的 pub_name 列包含空值。使用右向外联接
    假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。SQL-92 右向外联接运算符 RIGHT OUTER JOIN 指明:不管第一个表中是否有匹配的数据,结果将包含第二个表中的所有行。若要在结果中包括所有的出版商,而不管城市中是否还有出版商居住,请使用 SQL-92 右向外联接。下面是 Transact-SQL 右向外联接的查询和结果:USE pubs
    SELECT a.au_fname, a.au_lname, p.pub_name
    FROM authors AS a RIGHT OUTER JOIN publishers AS p
    ON a.city = p.city
    ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC下面是结果集:au_fname au_lname pub_name 
    -------------------- ------------------------ -------------------- 
    Abraham Bennet Algodata Infosystems
    Cheryl Carson Algodata Infosystems
    NULL NULL Binnet & Hardley
    NULL NULL Five Lakes Publishing
    NULL NULL GGG&G
    NULL NULL Lucerne Publishing
    NULL NULL New Moon Books
    NULL NULL Ramona Publishers
    NULL NULL Scootney Books(9 row(s) affected)使用谓词(如将联接与常量比较)可以进一步限制外联接。下例包含相同的右向外联接,但消除销售量低于 50 本的书籍的书名:USE pubs
    SELECT s.stor_id, s.qty, t.title
    FROM sales s RIGHT OUTER JOIN titles t
    ON s.title_id = t.title_id
    AND s.qty > 50
    ORDER BY s.stor_id ASC下面是结果集:stor_id qty title 
    ------- ------ --------------------------------------------------------- 
    (null) (null) But Is It User Friendly? 
    (null) (null) Computer Phobic AND Non-Phobic Individuals: Behavior 
    Variations 
    (null) (null) Cooking with Computers: Surreptitious Balance Sheets 
    (null) (null) Emotional Security: A New Algorithm 
    (null) (null) Fifty Years in Buckingham Palace Kitchens 
    7066 75 Is Anger the Enemy? 
    (null) (null) Life Without Fear 
    (null) (null) Net Etiquette 
    (null) (null) Onions, Leeks, and Garlic: Cooking Secrets of the 
    Mediterranean 
    (null) (null) Prolonged Data Deprivation: Four Case Studies 
    (null) (null) Secrets of Silicon Valley 
    (null) (null) Silicon Valley Gastronomic Treats 
    (null) (null) Straight Talk About Computers 
    (null) (null) Sushi, Anyone? 
    (null) (null) The Busy Executive's Database Guide 
    (null) (null) The Gourmet Microwave 
    (null) (null) The Psychology of Computer Cooking 
    (null) (null) You Can Combat Computer Stress! (18 row(s) affected)有关谓词的更多信息,请参见 WHERE。 使用完整外部联接
    若要通过在联接结果中包括不匹配的行保留不匹配信息,请使用完整外部联接。Microsoft® SQL Server™ 2000 提供完整外部联接运算符 FULL OUTER JOIN,不管另一个表是否有匹配的值,此运算符都包括两个表中的所有行。假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。SQL-92 FULL OUTER JOIN 运算符指明:不管表中是否有匹配的数据,结果将包括两个表中的所有行。若要在结果中包括所有作者和出版商,而不管城市中是否有出版商或者出版商是否住在同一个城市,请使用完整外部联接。下面是 Transact-SQL 完整外部联接的查询和结果:USE pubs
    SELECT a.au_fname, a.au_lname, p.pub_name
    FROM authors a FULL OUTER JOIN publishers p
    ON a.city = p.city
    ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC下面是结果集:au_fname au_lname pub_name 
    -------------------- ---------------------------- -------------------- 
    Reginald Blotchet-Halls NULL
    Michel DeFrance NULL
    Innes del Castillo NULL
    Ann Dull NULL
    Marjorie Green NULL
    Morningstar Greene NULL
    Burt Gringlesby NULL
    Sheryl Hunter NULL
    Livia Karsen NULL
    Charlene Locksley NULL
    Stearns MacFeather NULL
    Heather McBadden NULL
    Michael O'Leary NULL
    Sylvia Panteley NULL
    Albert Ringer NULL
    Anne Ringer NULL
    Meander Smith NULL
    Dean Straight NULL
    Dirk Stringer NULL
    Johnson White NULL
    Akiko Yokomoto NULL
    Abraham Bennet Algodata Infosystems
    Cheryl Carson Algodata Infosystems
    NULL NULL Binnet & Hardley
    NULL NULL Five Lakes Publishing
    NULL NULL GGG&G
    NULL NULL Lucerne Publishing
    NULL NULL New Moon Books
    NULL NULL Ramona Publishers
    NULL NULL Scootney Books(30 row(s) affected) 
      

  15.   

    用的是access与用SQL都是Transact-SQL语句。不同的只是一些函数。
    可以这样调试:
    在ACCESS中用SQL窗口测试SQL语句,成功后再加入前台程序。
      

  16.   

    to:  ovisa(小欧) 
    '这个我是在ACCESS中建表测试通过的.你的ACCESS是什么版本?'ACCESS中应该这样写:SELECT a.tabl_name
    FROM ((tab1 AS a INNER JOIN tab2 AS b ON a.tabl2_id = b.tab2_id) INNER JOIN tab3 ON b.tabl3_id = tab3.tab3_id) INNER JOIN tab4 AS d ON tab3.tab4_id = d.tab4_id
    where d.my_id=1
      

  17.   

    '是不是因为你没有注意我的条件,我用了d.my_id=1'你给的条件是d.my_id=3,改一下就行了.SELECT a.tabl_name
    FROM ((tab1 AS a INNER JOIN tab2 AS b ON a.tabl2_id = b.tab2_id) INNER JOIN tab3 ON b.tabl3_id = tab3.tab3_id) INNER JOIN tab4 AS d ON tab3.tab4_id = d.tab4_id
    where d.my_id=3
      

  18.   

    还要看你的关联的方式,这里仅给出内联作为参考:
    select a.tab1_name
    from tab1 a join 
              ( table2 b join 
                ( table3 c join table4 d 
                  on c.tab4_id =d.tab4_id 
                 ) on b.tab3_id=c.tab3_id
               ) on a.tab2_id=b.tab2_id
    where d.my_id=3这个SQL语句在SQL Server和Access中都能运行。有问题的话贴出错误信息。