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";
Related
I have looked for a similar issue with no luck. Maybe I don't know the right term to search for.
This seems so simple, but I just can't get it after spending many hours trying different approaches.
I have a dropdown to select contracts which shows some ids for related fields. How can I get those IDs to show the value of another column.
SELECT tbl_contracts.ID, tbl_contracts.contract_name, tbl_contracts.firm_id, tbl_contracts.agency_id
FROM tbl_contracts;
image of dropdown
I would like the IDs shown for agency_id and firm_id to list the company_name from their respective table "tbl_firm_agencies" where the tbl_contracts looks them up from. I've tried INNER JOINS but when I do, I can only line items to show when both agency AND firm exist, so my dropdown get cut off quite a bit.
Simply LEFT JOIN to those lookup tables as opposed to INNER JOIN. Adjust table and field names to actual ones in query below. Also, the parentheses are required in MS Access SQL.
SELECT c.ID, c.contract_name, f.firm_name, a.agency_name
FROM (tbl_contracts c
LEFT JOIN tbl_firms f
ON c.firm_id = f.firm_name)
LEFT JOIN tbl_agencies a
ON c.agency_id = a.agency_name;
Early last year I was on a project using Oracle DB and was introduced to a new querying format where you could query the results of the previous query. It was only for a few weeks we were helping on the project so so I don't recall exactly how things were written. But, it was something like the outline below. Note all of the query I believe was written in a stored procedure and in just 1 procedure. Forgive me for the rude formatting but I just cannot recall how things were just that I found it awesome the ability to do the query of queries and not have all the nested selects in one statement.
e.g. SP: X
select firstName from users where active = true;
select authors from books where authorFirstName in (previous select);
Any guidance on what this style of querying is called that would help me research this would be greatly appreciated as I would like to learn more on it and follow the format more.
You can use the SQL with clause to give a sub-query a name and then use that name. Example here:
SQL WITH clause example
the form you mentioned is subquery,
which may be wrote with joins (depends on the query and subqueries):
select firstName from users where active = true;
select authors from books where authorFirstName in (previous select);
is equal to:
select books.authors
from books
join users on books.authorFirstName =users.firstName
where users.active = true;
or equal to another subquery:
select authors
from books
where exists (select firstName
from users
where
books.authorFirstName =users.firstName
and active = true);
you can also use with statement:
with cte as (
select firstName from users where active = true)
select authors from books where authorFirstName in (select firtsname from cte);
and other forms ....
This is called an subquery.
The syntax usually is as follows:
select authors from books where authorFirstName in (select firstName from users where active = true);
Similar to that are the inline view:
select authors from books join
(select firstName from users where active = true) users2 on users2.firstname = authors.authorfirstname;
and with clause
with (select firstName from users where active = true) as users2
select authors from books where authorsfirstname = users2.firstname;
All have different advantages and usages.
I am writing a query to fill a select box on a user form and it works fine but when I look at the SQL, I feel like there should be a better, more efficient way to write it. Notice the two nested SELECTs below are from the same table.
I am wondering if there is an SQL trick I can add to my repertoire since I have to do queries like this from time to time. Or perhaps my approach is sloppy by some standards? Any feedback is appreciated.
Here is the SQL:
SELECT c.id, c.county_name, adr.county
FROM (SELECT id, county_name FROM counties WHERE state = (SELECT state FROM members WHERE id = 53)) AS c
LEFT JOIN (SELECT county FROM members WHERE id = 53) AS a ON c.id = a.county;
Here are the results (partial):
http://i.stack.imgur.com/Jawfa.png
The "3022" in the right column is member 53's county.
If needed, here is an explanation of the query and my intentions:
I have a members table where the county field is stored as an integer linking to a lookup table called counties. I also want to filter the county results to the member's state.
The query is filling an HTML select element and I want to pre-select the county of the member. The only information my PHP has available (without running a second query to get more) is the member id.
So the query needs to return the county id, the county name and some kind of identifier to let me know which record is matching for the member (I just went with the linking county field and my code will check for that).
The database is an MDB file accessed from a PDO ODBC connection.
You may need not need any subqueries but possibly a self-join:
SELECT counties.id, counties.county_name, members_1.county
FROM counties
INNER JOIN members ON counties.state = members.state
LEFT JOIN members_1 ON counties.id = members_1.county
WHERE members.id = 53 AND members_1.id = 53
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
I am trying to use a second SELECT to get some ID, then use that ID in a second SELECT and I have no idea how.
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
This post got long, but here is a short "tip"
While the syntax of my select is bad, the logic is not. I need that "x" somehow. Thus the second select is the most important. Then I have to use that "x" within the first select. I just don't know how
/Tip
This is the only thing I could imagine, I'm very new at Sql, I think I need a book before practicing, but now that I've started I'd like to finish my small program.
EDIT:
Ok I looked up joins, still don't get it
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
LEFT JOIN Distribution ON
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
Get error msg at AS and Left
I use name to find ID from upper red, I use the ID I find FROM upper red in lower table. Then I match the ID I find with Green. I use Green ID to find corresponding Name
I have California as output data from C#. I want to use California to find the DistributionID. I use the DistributionID to find the EmployeeID. I use EmployeeID to find Name
My logic:
Parameter: Distribution.Name (from C#)
Find DistributionID that has Distribution.Name
Look in Employment WHERE given DistributionID
reveals Employees that I am looking for (BY ID)
Use that ID to find Name
return Name
Tables:
NOTE: In this example picture the Employee repeats because of the select, they are in fact singular
In "Locatie" (middle table) is Location, I get location (again) from C#, I use California as an example. I need to find the ID first and foremost!
Sory they are not in english, but here are the create tables:
Try this:
SELECT angajati.Nume
FROM angajati
JOIN angajari ON angajati.AngajatID = angajari.AngajatID
JOIN distribuire ON angajari.distribuireid = distribuire.distribuireid
WHERE distribuire.locatie = 'california'
As you have a table mapping employees to their distribution locations, you just need to join that one in the middle to create the mapping. You can use variables if you like for the WHERE clause so that you can call this as a stored procedure or whatever you need from the output of your C# code.
Try this solution:
DECLARE #pLocatie VARCHAR(40)='Alba'; -- p=parameter
SELECT a.AngajatID, a.Nume
FROM Angajati a
JOIN Angajari j ON a.AngajatID=j.AngajatID
JOIN Distribuire d ON j.DistribuireID=d.DistribuireID
WHERE d.Locatie=#pLocatie
You should add an unique key on Angajari table (Employment) thus:
ALTER TABLE Angajari
ADD CONSTRAINT IUN_Angajari_AngajatID_DistribuireID UNIQUE (AngajatUD, DistribuireID);
This will prevent duplicated (AngajatID, DistribuireID).
I don't know how you are connecting Emplyee(sic?) and Employment, but you want to use a join to connect two tables and in the join specify how the tables are related. Joins usually look best when they have aliases so you don't have to repeat the entire table name. The following query will get you all the information from both Employment and Distribution tables where the distribution location is equal to california. You can join employee to employment to get name as well.
SELECT *
FROM Employment e
JOIN Distribution d on d.DistributionID = e.DistributionID
WHERE d.Location = 'California'
This will return the contents of both tables. To select particular records use the alias.[Col_Name] separated by a comma in the select statement, like d.DistributionID to return the DistributionID from the Distribution Table