简体   繁体   中英

TSQL union all - error

I'm using the following query to pull some information from 2 tables to populate a drop-down field on my website with "Category" values.

I'm getting the following error.

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

This is my query:

select 'Choose a Category', 'All'

Union All

select distinct CategoryName 
from BND_Listing
inner join BND_listingCategories on BND_Listing.CatID = BND_ListingCategories.CatID

I suspect it has something to do with the inner join?

Any input appreciated!

select 'Choose a Category' as CategoryName
Union All
select 'All' as CategoryName
Union All
select distinct CategoryName from BND_Listing
inner join BND_listingCategories
on BND_Listing.CatID=BND_ListingCategories.CatID

or

select 'Choose a Category' as CategoryName, 'All' as Value
Union All
select distinct CategoryName, CategoryName as Value from BND_Listing
inner join BND_listingCategories
on BND_Listing.CatID=BND_ListingCategories.CatID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM