2013-05-03 69 views
1

我想從數據庫中獲取一些數據。從3個表中選擇數據

  1. username從接觸表

登錄表

  • email在兩個表tutorinstitute

    與2個值檢查這是到目前爲止我的代碼:

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    

    此查詢不起作用,並且有錯誤消息。

    1054 - 在 '字段列表'

    UPDATE

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         )s 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    
  • +0

    你確定用戶名字段存在於登錄表中嗎? – ankurtr 2013-05-03 12:21:10

    +1

    你確定用戶名在研究所和導師表中存在嗎? – xQbert 2013-05-03 12:21:46

    +0

    http://stackoverflow.com/questions/16354226/make-a-select-query-from-two-table-with-where-clause ??? – nvanesch 2013-05-03 12:22:32

    回答

    1

    因爲它似乎要檢索您的usernamelogin,列username最有可能不tutors和/或institutes存在,也沒有必要,因爲你是在login_id加盟加盟login ,我想你可以只從子查詢中刪除username列:

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            --username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            --username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) s 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    

    我還添加別名s您subuqery因爲我以爲我T的遺漏是一個錯字,因爲它會在沒有拋出一個語法錯誤

    +0

    別名是type。你的解決方案是好的。它的工作現在。非常感謝你。 – TNK 2013-05-03 12:42:44

    +0

    非常歡迎。 – GarethD 2013-05-03 12:43:32

    -1

    這是在子查詢中username場未發現未知列 '用戶名',你必須在子查詢中包含login表。

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            l1.username, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors t 
         INNER JOIN login l1 ON l1.login_id = t.login_id 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            l2.username, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes i 
         INNER JOIN login l2 ON l2.login_id = i.login_id 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    
    +0

    它不適合我.. – TNK 2013-05-03 12:29:36

    +0

    好的@TNK我忘記了'ON'關鍵字和'我'在研究所'INNER JOIN',我修改了答案,你可以再試一次嗎? – 2013-05-03 12:43:49

    0

    沒有必要從tutorsinstitutes表調用username和使用as abc當你的子查詢關閉像

    SELECT s. * , c.email, l.username 
    FROM (
         SELECT contact_id AS id, 
            login_id, 
            tutor_code AS code, 
            tutor_name AS Name, 
            'tutor' AS profile 
         FROM tutors 
         WHERE tutor_code = $code AND tutor_name = '$name' 
         UNION ALL 
         SELECT contact_id AS id, 
            login_id, 
            institute_code AS code, 
            institute_name AS Name, 
            'institute' AS profile 
         FROM institutes 
         WHERE institute_code = $code AND institute_name = '$name' 
         ) as s 
    INNER JOIN contact c ON s.id = c.contact_id 
    INNER JOIN login l ON s.login_id = l.login_id 
    

    這個查詢將返回重複條目,因爲您在union中使用ALL

    希望它適合你。

    +1

    如果你將子查詢別名爲'abc',你還需要更新別名's'的使用位置('s。*','s.login_id = l.login_id','s.id = c.contact_id ') – GarethD 2013-05-03 12:39:16

    +0

    @GarethD是的你需要把's'更新爲'abc',但我把'abc'更改爲's' ..感謝你的建議.. – Rahul 2013-05-03 12:44:00