简体   繁体   中英

How can I select a category and the first picture associated with that category?

What I'm trying to do: I want a grid of picture categories to be generated from the categories in my database. So I'm pulling all of the categories with this SQL command.

SELECT * FROM Category

But I also want a picture to show up in the listview so I tried to add this subquery to it

SELECT * FROM Category 
INNER JOIN Pictures ON Category.CategoryId = Pictures.CategoryId
(SELECT TOP 1 Pictures.PictureFilePath FROM Pictures
 WHERE Catergory.CategoryId = Pictures.CategoryId)

When I try to test the query it tells me I have incorrect syntax near "SELECT" and ")"

So my question is either how do I fix this query, or how can I use LINQ to populate my listview?

SELECT 
  Category.*, 
  (
    SELECT Top 1 Pictures.PictureFilePath 
    FROM Pictures 
    WHERE Category.CategoryID = Pictures.CategoryID 
  )
FROM
  Category

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-2024 STACKOOM.COM