I am trying to create an SQL query for an ASP NET drop down list using two tables and a session variable.
I want to retrieve the all values from table 1 which correspond to a matching ids in table 2, where table 2 is filtered by an external variable.
As it is clear I do not know how to word this question, here is a simplified example of what I am attempting to do:
My site has a session variable which holds the current colour the user is "filtering".
A dropdown list will show a list of cars corresponding to that colour using an SQL query.
For example if the session variable was "Blue" the dropdown list would contain "Punto" as it can see that the colour ID for "Blue" is 12 and "Punto" is the only car name corresponding to that colour.
Linked image: http://i.imgur.com/fe9L12c.png
As session variables can be assigned and called in ASP NET custom queries the session variable can just be referred to as, for example, #ExternalVar (Colours.ID WHERE (Colours.Name = #ExternalVar))
Apologies I had to word this like a quiz question; giving a simplified example was the only way I could really articulate my question.
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
SELECT Cars.* FROM Colours
INNER JOIN Cars
ON Colours.ID = Cars.ColourID
WHERE Colours.Name = #Variable
You can achieve this by using SQL Joins. 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
Related
Let's say I have a table animal and a table competition with 1:n relationship.
Table animal looks like:
id_animal
type
1
cat
2
dog
Table competition looks like:
id_competition
fid_animal
name
1
1
A
2
2
A
3
2
B
This means the cat is participating in competition A and the dog is participating in competition A and B.
I now want to filter the table animal based on the entries in table competition.
The query sounds like:
Show me all animals (type), that participate in competition (name) A but not B. The result should give me only the cat, not the dog.
I tried my luck with following sql (simplified):
Select DISTINCT * FROM animal
LEFT JOIN competition ON id_animal = fid_animal
WHERE competition.name IN ("A") AND NOT competition.name IN ("B");
As result I get both, cat and dog.
Only AND NOT returns me the dog, which is fine but both together don't give me the desired result.
What am I doing wrong? How can I achieve the output?
Thank you in advance!
Background:
I have a bound form (datatable) with lot of entries and a table as data source. The user should be able to filter the form based on parameters in a related (1:n) table. So I made an unbound form with listboxes (populated with possible entries of the related table), where the user can choose multiple criteria. The result is a sql statement like above which is used as new data source for the bound form. This logic is working fine. Problem is that sometimes an entry in the form can have multiple parameters and for the user it is important to filter by chosen parameters but exclude the result if it contains specific other parameters. There are a few related tables, many listboxes are part of the filter and the sql string is generated dynamically, based on if and what the user selects.
MS Access is usually far from standard compliant. The following is the simple query in standard SQL and runs in about every RDBMS:
SELECT *
FROM animal
WHERE id_animal IN (SELECT id_animal FROM competition WHERE name = 'A')
AND id_animal NOT IN (SELECT id_animal FROM competition WHERE name = 'B');
[NOT] IN works on lists. It makes little sense to use it on single-item lists. name IN ("A") is the same as name = "A". As you want to look up the animal list in the competition table, you need a subquery as shown above.
As you see, I don't join. Why should I? I want to select animals, so I select from the animal table. I want to restrict the result to certain animals, so I use a WHERE clause.
I notice you are using double quotes for string literals. If this is necessary in MS Access, then replace the single quotes above with double quotes. (In standard SQL double quotes delimit names, not string literals, but this may be different in MS Access.)
Consider:
SELECT animal.* FROM animal LEFT JOIN competition ON id_animal = fid_animal
WHERE [name]="A" AND NOT id_animal IN (SELECT fid_animal FROM competition WHERE [name]="B")
Might find this related question of interest Microsoft Access - Filtering a form based on check boxes for one field
You need to look at several rows. Your predicate:
WHERE competition.name IN ("A") AND NOT competition.name IN ("B");
is evaluated against the same row. It will find all rows where name is 'A' and not 'B', i.e. competition 1 and 2. If you instead look at the rows with name 'A' where it does not exists a row for the same animal with name 'B' you will find what you are looking for:
SELECT c1.fid_animal
FROM competition c1
WHERE c1.name = 'A'
AND NOT EXISTS (
SELECT *
FROM competition c2
WHERE c1.fid_animal = c2.fid_animal
AND c2.name = 'B'
)
Now you can join this result with animal to find the type:
SELECT a.type
FROM competition c1
JOIN animal a
ON a.id_animal = c1.fid_animal
WHERE c1.name = 'A'
AND NOT EXISTS (
SELECT *
FROM competition c2
WHERE c1.fid_animal = c2.fid_animal
AND c2.name = 'B'
);
FWIW, type is a reserved word in SQL so you should avoid using that as an identifier.
I have made a table with placements.
It says
New York
Washington
I use the table in another table and form where the user can select a city.
But when I try to query the selected value with SQL, I can only search on the ID.
Like
SELECT Articles.Name
FROM Articles
WHERE (((Articles.Placement.Value)=1);
I would like to search by name instead. How is that done? I simply can't find any info on that anywhere.
SELECT Articles.Name
FROM Articles
WHERE (Articles.Placement="New York");
I think you just want a JOIN:
SELECT a.Name
FROM Articles as a INNER JOIN
Placements as p
ON a.Placement_ID = p.Placement_Id
WHERE p.Placement = "New York";
I have two tables. Table "List" and table "Content". I want to store 6 types of lists. These lists are black,white and grey lists and each of these list contain a few words. Whenever someone notices a new word that should be in one of the lists, then new word should be simply added to the database.
The image below shows you the tables that I use.
I want to refer or join a certain list for instance a list with name: "Blacklist" that has an ListID= 1 with the correct set of blacklistwords, that could have a ContentID=1.
The content is a list of words, but I am clueless as for how I should join the correct list of words(content) to a listID. I don't know how to query this.
The part that is troubeling me is that it is a list of words. So a ContentID =1 has for example the words"Login","Password", "Credential" etc. How do I query it to ListID=1 with the name"BlackList"? And do the same for the other lists?
I think it should look like this.
SELECT ID
FROM List
LEFT JOIN Content
ON LIST.ID = ContenID AND CONTENT.ISDEFAULT = 1
WHERE ListID = 1
This only joins the two ID with each other. How do I join the correct list of words with the correct list? Maybe I am totally missing the point with the query above?
Question: How do I join a set or list of words to a list with a name and ListID?
Once you change this schema, the below query will work
SELECT ListID,ContentID,Words
FROM List
LEFT JOIN Content
ON List.ListID = Content.ListID
WHERE List.ListID = 1
I have considered the schema from the diagrams. Please execute the below query:
SELECT
L.Name AS 'ListName',
C.Words
FROM List L
INNER JOIN Content C ON C.ListID = L.ListID
WHERE
C.Words IN ('Login','Password','Credential')
I currently have a schema set up in the following manner:
The table tblCategoryRiskArea is set up as an intermediate table for the many-to-many relationship that can exist between Categories and RiskAreas.
Within the tblBase table, I would like to make it so that the RiskArea choices are dependant upon the Category choice. MS Access allows you to set a Lookup for a field in a table based upon a Row Source SQL statement. I am having trouble figuring out the correct SQL statement to define the Row Source for RiskArea dependant upon Category. This:
SELECT tblRiskAreas.RiskAreaID, tblRiskAreas.RiskArea
FROM tblRiskAreas INNER JOIN
((tblCategories INNER JOIN tblBase
ON tblCategories.CategoryId = tblBase.Category)
INNER JOIN tblCategoryRiskArea
ON tblCategories.CategoryId = tblCategoryRiskArea.Category)
ON (tblRiskAreas.RiskAreaID = tblCategoryRiskArea.RiskArea)
AND (tblRiskAreas.RiskAreaID = tblBase.RiskArea)
WHERE (((tblCategoryRiskArea.Category)=[tblBase]![Category]))
ORDER BY tblRiskAreas.RiskAreaID;
is the best I've come up with so far, using MS Access' Query Builder, so all of the Inner Joins have been created just by my having defined the relationships between the tables and dragging them into the Query Builder. This query returns nothing, however.
I suspect that it may have something to do with the circular nature of the relationships I set up?
Thank you.
Edited: tblRiskArea contains 4 RiskAreas, as follows:
Environmental
Health
Safety
Security
Each Category can fall into one or two of these RiskAreas, so the tblCategoryRiskArea creates the relationship bewtween them.
First remove Category and RiskArea from tblBase and replace them for CategoriRiskAreaID
You will show in your form 2 combos. First combo Catagory data source:
Select CategoryId,Category from tblCategories
Second combo Risk Areas data source:
Select a.CategoryRiskId, b.RiskArea
from tblCategoryRiskArea a
inner join tblRiskArea b
where a.RiskAreaId=b.RiskAreaId
AND a.category = #ComboBoxCategorySelectedItem
Now you have the value to insert in tblBase, ComboBoxSelectedItem is tblCategoryRiskArea.CategoryRiskId
i hava a set of following tables
customer(cus_id,cus_name);
jointAccount(cus_id,acc_number,relationship);
account(acc_number,cus_id)
now i want to create a select statement to list all the jointAccounts,
it should included the both customer name, and relationship.
I have no idea how to retrieve both different user name, is that possible to do this?
Generally speaking, yes. I'm assuming you mean you want to get customer info for both sides of the joint account per your jointAccount table. Not sure what database you're using so this answer is assuming MySQL.
You can join on the same table twice in a single SQL query. I'm assuming you have not yet created your tables, as you have cus_id listed twice in the jointAccount table. Typically these would be something like cus_id1 and cus_id2, which I've used in my sample query below.
Example:
SELECT c1.cus_id AS cust1_id, c1.cus_name AS cust1_name
, c2.cus_id AS cust2_id, c2.cus_name AS cust2_name, j.relationship
FROM customer c1
INNER JOIN jointAccount j
ON c1.cus_id = j.cus_id1
, customer c2
INNER JOIN jointAccount j
ON c2.cus_id = j.cus_id2
I haven't tested this but that's the general idea.
try this query:
SELECT * FROM jointAccount a LEFT JOIN customer c ON a.cus_id = c.cus_id;
just replace the * with the name of the columns you need.