SQL Query - SELECT WHERE Table1.ID = Table2.ID AND Table2.Var = @Var

I am trying to create a SQL query for an ASP NET dropdown using two tables and a session variable.

I want to get all the values ​​from table 1 that match the match IDs in table 2 where table 2 is filtered by an external variable.

As is clear, I don't know how to frame this question, here is a simplified example of what I am trying to do:

  • My site has a session variable that contains the current color that the user "filters".
  • The dropdown will display a list of cars matching that color using an SQL query.

For example, if the session variable was Blue, the dropdown would contain Punto because he can see that the color ID for Blue is 12 and Punto is the only car name that matches that color.

Associated image: http://i.imgur.com/fe9L12c.png

enter image description here

Since session variables can be assigned and called in custom ASP.NET requests, the session variable can be simply named, for example @ExternalVar (Colors.ID WHERE (Colors.Name = @ExternalVar))

Apologies, I had to say this as a quiz question; by giving a simplified example, I was the only way I could formulate my question.

+3


source to share


3 answers


I think this should do the trick if I understand the question above



select * from Cars c
inner join Colours cl  on c.colourID = cl.ID
where cl.Name = @ExternalVar

      

+4


source


SELECT Cars.* FROM Colours 
INNER JOIN Cars 
ON Colours.ID = Cars.ColourID
WHERE Colours.Name = @Variable

      



0


source


You can achieve this using SQL connections. Use below sql query: -

Declare @ExternalVar VARCHAR (50) = 'Blue'

Select C.Name FROM Cars C INNER JOIN Colours CO ON CO.ID = C.ColourId WHERE CO.Name = @ExternalVar

      

0


source







All Articles