2013-11-22 88 views
1

我試圖找到凡人有一定的地址。SQL查找地址?

我有我認爲是正確的代碼,但無論我對'WHERE'子句做什麼,都得不到返回的結果。

的人S.

住在雅法4389臺創建的姓和名的唯一列表我已經試過

SELECT MORTAL.FIRST_NAME||' '||MORTAL.LAST_NAME "4389_Jaffa_Terrace_S" 
FROM MORTAL 
JOIN MORTAL_ADDRESS ON MORTAL.MORTAL_ID = MORTAL_ADDRESS.MORTAL_ID 
JOIN ADDRESS ON MORTAL_ADDRESS.ADDRESS_ID = ADDRESS.ADDRESS_ID 
WHERE ADDRESS.ADDRESS_LINE1 LIKE '%4389%'; 


SELECT FIRST_NAME||' '||LAST_NAME "4389_Jaffa_Terrace_S" 
FROM MORTAL 
JOIN MORTAL_ADDRESS ON MORTAL.MORTAL_ID = MORTAL_ADDRESS.MORTAL_ID 
JOIN ADDRESS ON MORTAL_ADDRESS.ADDRESS_ID = ADDRESS.ADDRESS_ID: 
WHERE ADDRESS.ADDRESS_LINE1 LIKE '%4389%'; 

SQL> SELECT FIRST_NAME||' '||LAST_NAME "4389_Jaffa_Terrace_S" 
    2 FROM MORTAL 
    3 JOIN MORTAL_ADDRESS ON MORTAL.MORTAL_ID = MORTAL_ADDRESS.MORTAL_ID 
    4 JOIN ADDRESS ON MORTAL_ADDRESS.ADDRESS_ID = ADDRESS.ADDRESS_ID 
    5 WHERE UPPER(ADDRESS.ADDRESS_LINE1) LIKE '%4389%'; 

no rows selected 

SQL> SELECT FIRST_NAME||' '||LAST_NAME "4389_Jaffa_Terrace_S" 
    2 FROM MORTAL 
    3 JOIN MORTAL_ADDRESS ON MORTAL.MORTAL_ID = MORTAL_ADDRESS.MORTAL_ID 
    4 JOIN ADDRESS ON MORTAL_ADDRESS.ADDRESS_ID = ADDRESS.ADDRESS_ID 
    5 WHERE UPPER(ADDRESS.ADDRESS_LINE1) LIKE '%JAFFA%'; 

SQL> SELECT FIRST_NAME||' '||LAST_NAME "4389_Jaffa_Terrace_S" 
    2 FROM MORTAL 
    3 JOIN MORTAL_ADDRESS ON MORTAL.MORTAL_ID = MORTAL_ADDRESS.MORTAL_ID 
    4 JOIN ADDRESS ON MORTAL_ADDRESS.ADDRESS_ID = ADDRESS.ADDRESS_ID 
    5 WHERE SUBSTR(ADDRESS.ADDRESS_LINE1,0,4) LIKE '4389'; 

no rows selected 

編輯補充:

SQL> desc mortal 
Name 
-------------------------- 
MORTAL_ID 
SEX_TYPE_CODE 
FIRST_NAME 
LAST_NAME 
DOB 
MARITAL_STATUS_CODE 
SSN 
MIDDLE_NAME 
WORK_PHONE 

SQL> desc Mortal_address 
Name 
-------------------------- 
ADDRESS_ID 
MORTAL_ID 

SQL> desc address 
Name 
-------------------------- 
ADDRESS_ID 
ADDRESS_TYPE_CODE 
ZIP_CODE 
STATE_CODE 
ADDRESS_LINE1 
CITY 
ADDRESS_LINE2 
ADDRESS_LINE3 
COUNTRY 
+2

如果沒有where子句,你會得到什麼嗎?您的連接是否正確/ – smk

+0

您能向我們展示MORTAL和MORTAL_ADDRESS的DDL以及應該返回的那些表中的數據嗎?這將有助於確定問題。 – MichaelMilom

+0

好吧,堅持,我編輯我的帖子 –

回答

3

嘗試逐步調試。

第一:

select * from address where address_line1 like '%4389%' 

那麼,對於任何地址返回,

select * from Mortal_address where address_id = <the address_id that was returned from above> 

然後,

select * from mortal where mortal_id = <the mortal_id that was returned from above> 

也許這將顯示在您的連接不正確加盟?

+0

我的步驟會稍有不同。第一個查詢後,我會重複它,但加入。順便說一句,我對這個問題的投票將你的聲望推到了4位數。你欠我一杯啤酒。 –

+0

@DanBracuk - 同意你的想法......並且,感謝你的提升 - 將它添加到列表中:http://youowemeabeer.com/ – BWS

+0

他們都工作得很好。 –