2010-06-15 23 views
2

我有一個數據庫情況,我希望按用戶年齡範圍獲取用戶配置文件行。獲取兩列值之間的數據庫結果

這是我的分貝:

table_users 
username  age  email      url 
pippo  15  [email protected]  http://example.com 
pluto  33  [email protected]  http://example.com 
mikey  78  [email protected]  http://example.com 


table_profiles 
p_name  start_age_range  stop_age_range 
young  10     29 
adult  30     69 
old   70     inf 

我用MySQLPHP,但我不知道是否有一些具體的tacnique做到這一點,如果有可能,當然。

# so something like: 
SELECT * 
FROM table_profiles AS profiles 
INNER JOIN table_users AS users 
# can I do something like this? 
ON users.age IS BETWEEN profiles.start_age_range AND profiles.stop_age_range 
+2

爲什麼存儲在數據庫中不斷變化的價值?不會出生日期更合適嗎? – symcbean 2010-06-15 11:18:53

+1

你有沒有嘗試從IS間刪除IS?使用SQL Server,它是一個有效的SQL語句。 – 2010-06-15 11:34:25

+0

@symcbean你是對的,我現在保存一個日期,我在這個例子中使用了一個簡單的數字來簡化我的答案。 – vitto 2010-06-15 11:48:04

回答

2
Select 
    * 
From 
    table_profiles As profiles, 
    table_users As users 
Where 
    users.age Between profiles.start_age_rage And profiles.stop_age_range 
+0

謝謝你,我以爲它就像BETWEEN子句 – vitto 2010-06-15 12:04:57

+0

你很受歡迎(: – 2010-06-15 12:18:31

0

什麼

SELECT * 
    FROM table_profiles AS profiles 
    Where 
users.age >= profiles.start_age_range AND users.age <=profiles.stop_age_range 
+0

'INNER JOIN

ON '對於任何數據庫都不是合法的SQL。 – 2010-06-15 11:25:39

+0

刪除謝謝。 – 2010-06-15 11:30:59

相關問題