Mavric Mavric - 1 year ago 57
ASP.NET (C#) Question

Displaying an item that meets 2 conditions in a SQL database in a Webform label

enter image description hereI'm completely new to SQL, so please bear with me.

I'm basically trying to display an item that meets 2 conditions within an SQL database in a Webform on Visual Studio using C#.

The table consists of words according to category and level(easy, medium, hard). The webform allows the user to select multiple categories using check boxes and the level via a drop down list.

I would like to display a single word within the database depending on which level and which category has been chosen.

I've attached a picture of what the table looks like.

Any help would be greatly appreciated.

Thank youenter image description here

Answer Source

Most of the reason you were struggling with a query here is because the data model is less than optimal. Here is better model for your words.

create table Words
    LevelID int
    , CategoryID int
    , Word varchar(50)

create table WordLevel
    LevelID int
    , LevelDescription varchar(25)

create table Category
    CategoryID int
    , CategoryDescription varchar(50)

insert WordLevel
select 1, 'Easy' union all
select 2, 'Medium' union all
select 3, 'Hard'

insert Category
select 1, 'flora' union all
select 2, 'space' union all
select 3, 'wildlife'

insert Words
select 1, 1, 'sunflower' union all
select 1, 2, 'pluto' union all
select 1, 3, 'lion'

select * --of course you would only include the columns you actually need here
from Words w
join Category c on c.CategoryID = w.CategoryID
join WordLevel wl on wl.LevelID = w.LevelID
where w.LevelID = 1
    and c.CategoryDescription = 'space'