Javascript 如何从两个表中选择一个属性?
朋友们,我有两个表BrandCategories和BrandOffers。两个表都有一个列名“brandId”。现在,我正在使用我在下面编写的两个独立查询来选择brandIdJavascript 如何从两个表中选择一个属性?,javascript,sql,Javascript,Sql,朋友们,我有两个表BrandCategories和BrandOffers。两个表都有一个列名“brandId”。现在,我正在使用我在下面编写的两个独立查询来选择brandId Select brandId from BrandCategories where categoryId = "selectedCategory" AND isDeleted=0 Select brandId from BrandOffers where discountType = "selectedDisc
Select brandId from BrandCategories where categoryId = "selectedCategory" AND isDeleted=0
Select brandId from BrandOffers where discountType = "selectedDiscountType"
我想在一个包含两个表的结果的查询中选择brandId??我怎么做
我尝试了以下书面查询
SELECT brandId FROM BRANDCATEGORIES INNER JOIN BRANDOFFERS on BRANDCATEGORIES.brandId = BRANDOFFERS.brandId where BRANDCATEGORIES.categoryId='+brandCategorySelected+' AND BRANDOFFERS.discountTypeArabic="'+$('#DiscountDrop').val()+'" AND BRANDCATEGORIES.isDeleted=0
请告诉我我是对还是错
在我的程序中,我编写了如下内容:
db.transaction(function(tx) {tx.executeSql('(SELECT brandId FROM BRANDCATEGORIES INNER JOIN BRANDOFFERS on BRANDCATEGORIES.brandId = BRANDOFFERS.brandId where BRANDCATEGORIES.categoryId='+brandCategorySelected+' AND BRANDOFFERS.discountTypeArabic="'+$('#DiscountDrop').val()+'" AND BRANDCATEGORIES.isDeleted=0)', [], testing, errorCB);}, errorCB);
如果要获得两个查询的不同值,请使用
union
;如果要获得两个查询的所有结果,请使用union all
(这可能会导致重复):
你可以用UNION来做这个
Select brandId from BrandCategories where categoryId = "selectedCategory"
UNION
Select brandId from BrandOffers where discountType = "selectedDiscountType"
UNION是一种书写UNION DISTINCE的较短方法。
如果要同时获取两个表中的键,请使用UNION ALL
Select brandId from BrandCategories where categoryId = "selectedCategory"
UNION ALL
Select brandId from BrandOffers where discountType = "selectedDiscountType"
告诉我们您是否得到了正确的结果。04-02 14:08:13.052:E/SQLiteLog(28112):(1)靠近“(”:语法错误我到目前为止收到了这个错误,我只想得到那些具有折扣类型=“selectedDiscountType”的brandId,但它们必须在类别中。Union正在合并整个结果我只想得到那些具有折扣类型=“selectedDiscountType”,但它们必须在类别中..Union正在合并整个结果-
Select brandId from BrandCategories where categoryId = "selectedCategory"
UNION ALL
Select brandId from BrandOffers where discountType = "selectedDiscountType"