2014-04-21 70 views
0
SELECT Distinct ListingId,FieldId,FieldValue 
    FROM [Chant-GreyPar].[dbo].[gp_listing_field] 
    where (FieldId = 54) and (FieldId = 69) 
     order by ListingId asc 

好傢伙選擇ID我有表從柱= 1和第2列MSSQL 2012

ListingId FieldId FieldValue 
238878 54 Paupackan Lake 
238878 69 N 
238879 54 None 
238879 69 N 
238880 54 Westcolang Lake 
238881 54 None 
238882 54 None 

,我需要選擇的ID了FieldId 54和69 ...需要幫助烏拉圭回合。

UPDATE:

select distinct l.Id,[SquareFeet],[HouseNumber],[StreetAddress],[PropertyTypeId],[Bedrooms],[Bathrooms],[ListingPrice], 
(select top 1 PhotoUrl from [Chant-GreyPar].dbo.gp_listing_photo where gp_listing_photo.ListingId = f.ListingId) AS PhotoUrl, 
(Select AreaName1 from [Chant-GreyPar].dbo.gp_location where gp_location.Id = l.LocationId) AS AreaName1, 
(Select AreaStateCode from [Chant-GreyPar].dbo.gp_location where gp_location.Id = l.LocationId) AS AreaStateCode 
from [Chant-GreyPar].dbo.gp_listing l inner join [Chant-GreyPar].[dbo].[gp_listing_field] f 
on f.ListingId = l.Id left join [Chant-GreyPar].dbo.gp_vw_DecimalListingField s on s.ListingId = l.Id 
where (l.DisplayListing='1' and f.FieldId='69' and f.FieldValue='Y') and (l.DisplayListing='1' and f.FieldId='15' and f.FieldValue='Window Unit AC' or f.FieldValue='Wall Unit AC' or f.FieldValue='Window Unit AC' or f.FieldValue='Central AC') 
and ListingPrice >= 0 and ListingPrice <= 99999999999 and Bedrooms >= 0 and Bathrooms >= 0 
and SquareFeet >= 0 and (FieldValueDecimal >= 0 or FieldValueDecimal is null) 
order by ListingPrice desc 

如何整合在這裏它。 Thnks。

+0

FieldId不可能是59 _和_ 64.你應該使用OR來代替(或者使用IN()條件) –

回答

1

好吧,如果你想要做什麼,我認爲你正在試圖做的,用這個:

SELECT DISTINCT ListingId, FieldId, FieldValue 
FROM [Chant-GreyPar].[dbo].[gp_listing_field] 
WHERE ListingId IN (
    SELECT ListingId 
    FROM [Chant-GreyPar].[dbo].[gp_listing_field] 
    WHERE FieldId IN (54, 69) 
    GROUP BY ListingId 
    HAVING Count(ListingId) = 2 
) 
ORDER BY ListingId ASC 

這將使用子查詢(SELECT語句的WHERE子句中),以獲取有2條記錄的所有ListingId列表。子查詢中的WHERE子句按您要查找的2個值進行過濾 - 54和69 - 因此它只會給您帶有54和69的記錄。主查詢然後使用該列表獲取值爲那些行。

您的更新有一點複雜,沒有數據庫在我面前。所有你應該做的就是使用我的子查詢來代替你正在搜索FieldId值的WHERE子句的第一部分。不保證我的語法是正確的在這裏:

USE [Chant-GreyPar] -- This eliminates the need to keep repeating it in the query 
GO 
SELECT DISTINCT l.Id, SquareFeet, HouseNumber, StreetAddress, PropertyTypeId, Bedrooms, Bathrooms, ListingPrice, 
    (SELECT TOP 1 PhotoUrl FROM dbo.gp_listing_photo WHERE gp_listing_photo.ListingId = f.ListingId) AS PhotoUrl, 
    (SELECT AreaName1 FROM dbo.gp_location WHERE gp_location.Id = l.LocationId) AS AreaName1, 
    (SELECT AreaStateCode FROM dbo.gp_location WHERE gp_location.Id = l.LocationId) AS AreaStateCode 
FROM dbo.gp_listing l 
INNER JOIN dbo.gp_listing_field f ON f.ListingId = l.Id 
LEFT JOIN dbo.gp_vw_DecimalListingField s ON s.ListingId = l.Id 
WHERE ListingId IN (
    SELECT ListingId 
    FROM dbo.gp_listing_field 
    WHERE (l.DisplayListing='1' AND f.FieldId='69' AND f.FieldValue='Y') 
    OR (l.DisplayListing='1' AND f.FieldId='15' AND f.FieldValue IN ('Window Unit AC', 'Wall Unit AC', 'Window Unit AC', 'Central AC')) 
    GROUP BY ListingId 
    HAVING Count(ListingId) = 2 
) 
AND ListingPrice BETWEEN 0 AND 99999999999 
AND Bedrooms >= 0 
AND Bathrooms >= 0 
AND SquareFeet >= 0 
AND (FieldValueDecimal >= 0 OR FieldValueDecimal IS NULL) 
ORDER BY ListingPrice DESC 

順便說一句,你從54變爲第二FieldId值15.不知道這是故意的。

+0

不,它沒有工作,已經嘗試 –

+0

你是說你需要得到ListingId只有鏈接到54和69記錄的地方? –

+0

yes.Nor或。我需要當fielid = 54和69 –

相關問題