I'm trying to make a query that grabs a single row from an SQL database and updates it.
TableA
AId
AValue
TableB
BId
AId
BValue
Ok, so TableA and TableB are linked by AId. I want to select a row in TableB based on AValue using a join. The following query is what I have and only grabs a value from TableB based on AId, I just don't know how to grab a row from TableB using AValue. I know you would need to use a join, but I'm not sure how to accomplish that.
var row = DbObject.TableB.Single(x => x.AId == 1)
row.BValue = 1;
DbObject.SubmitChanges();
Below is a LINQ query to do what you are asking.
var row = (from b in TableB
join a in TableA on a.AId equals b.AId
where a.AValue = yourValue).Single();
Related
Consider the query
var query = from A in _context.A.AsExpandable().Where(condition)
let B = _context.B
.Where(a=> a.Id == B.Id)
.Select(x=> new
{
x.Id,
x.Name
}).ToList()
select new {A.Id, B }
var res = query.ToList()
The Generated T-SQL similar to
SELECT [t0].Id, [s].Id, [s].Name
FROM A AS[t0]
LEFT JOIN [B] AS [s] ON [t0].[Id] = [s].[Id]
I Was wondering instead of joining the whole table is possible/better to join a subset(may be a inner join which just fetches two column instead of all the columns) of the table to improve performance?.
What it basically does it Left joins table A with table B, I'm trying to join the table A with the subset of table B (Id, Name) instead of selecting all the column.
Is it possible to achieve this??
would it improve the performance or degrade the performance?
Sectionname gradelevel and teachersid
How can use join table ? How can i link into the other table
Select a.sectionname AS sec b.gradelevel AS level c. Teachersid AS teacher
There are a few different ways that you can do this. However, in order to do this, you must have a foreign key in the table to be joined. You may be looking for something like the following example, but I cannot give you an exact answer without knowing the fields of the tables.
SELECT A.SECTIONNAME, B.GRADELEVEL, C.TEACHERSAID
FROM SECTIONNAME, GRADELEVEL, TEACHERSAID
WHERE SECTIONNAME.ID = GRADELEVEL.ID
AND SECTIONNAME.ID = B.GRADELEVEL.ID
AND GRADELEVEL.ID = C.TEACHERSAID.ID;
SELECT TableA.*, TableB.*, TableC.*, TableD.*
FROM TableA
JOIN TableB
ON TableB.id = TableA.id
JOIN TableC
ON TableC.id = TableB.id
JOIN TableD
ON TableD.id = TableA.id
I have 3 tables with various columns:
tableA = id(PK) & name columns
tableB = id(PK), A_ID(foreign key to tableA), name, address, etc columns
tableC = id(PK), A_ID(foreign key to tableA), name columns
I’m trying to use the following query to retrieve values from certain columns within all tables based on tableA name = ‘something’, but always returning syntax errors.
“SELECT tableA.name, tableB.name, tableB.address, tableC.name FROM
tableA, tableB, tableC JOIN tableB ON tableA.id = tableB.A_ID JOIN tableC
ON tableA.id = tableC.A_ID WHERE tableA.name = ‘something’”
You have to remove tables from from statement if you want to use join syntax
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM tableA
INNER JOIN tableB ON tableA.id = tableB.A_ID
INNER JOIN tableC ON tableA.id = tableC.A_ID
WHERE tableA.name = 'something'
I suggest you to use aliases, the code could be more readable:
SELECT A.name, B.name, B.address, C.name
FROM tableA A
INNER JOIN tableB B ON A.id = B.A_ID
INNER JOIN tableC C ON A.id = C.A_ID
WHERE A.name = 'something'
Ms Access requires you to specify the type of join: INNER; LEFT; or RIGHT. Access does not recognize just JOIN as a synonym for INNER JOIN.
A query which includes more than one join requires parentheses in the FROM clause.
I also changed your quote characters to plain " and '. The sample query included type-setting quotes. I don't know if they are present in the actual SQL, but I would avoid them.
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM
(tableA
INNER JOIN tableB
ON tableA.id = tableB.A_ID)
INNER JOIN tableC
ON tableA.id = tableC.A_ID
WHERE tableA.name = 'something'
If you have the full version of Access available, use the query designer to set up the joins. The designer knows the syntax rules which keep the db engine happy.
otherwise you could do it like:
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM tableA, tableB, tableC
WHERE tableA.id = tableB.A_ID AND tableA.id = tableC.A_ID
AND tableA.name = ‘something
Parado's answer is correct..
Moreover you can simplify the length of the query by using aliases for the table names.. And it is good practice too..
select a.name, b.name, b.address
from tableA a
join tableB b
on a.id = b.A_ID
I have a sql query which is to be converted to Linq
Select b.title
from TableA as a
inner join TableB as b
on a.Email=b.Email
where a.Title<>b.Title
the query which i have tried is
var query =from s in TableA
join r in TableB
on r.Email equals s.Email
but not able to replicate the where clause which may include many columns
My requirement is i need to compare the 2 tables on a primary key column and then get the other column values which do not match
You need a "select" at the end of the query, and you need to get the inputs in the right order:
var query = from s in TableA
join r in TableB on s.Email equals r.Email
where s.Title != r.Title
select s.Title;
For multiple columns, use an anonymous type:
var query = from s in TableA
join r in TableB
on new { s.Email, s.Foo } equals new { r.Email, r.Foo }
where s.Title != r.Title
select s.Title;
Lets say I have 2 tables both of them contain dynamic columns and I wish to retrieve a collection of datarow with all the columns from both the tables(later i will bind it to a grid view) after performing left outer join.
Sample Query:
var query = from TableA in ds.Tables[0].AsEnumerable()
join TableB in ds.Tables[1].AsEnumerable() on new { col1 = TableA.Field<Int32>("colA"), col2 = TableA.Field<DateTime>("colB") }
equals new { col1 = TableB.Field<Int32>("colA"), col2 = TableB.Field<DateTime>("colB") }
into GJ
from sub in GJ.DefaultIfEmpty()
select TableA;
Problem:
I want to select tableA and tableB together. The above sample query works and it populates all columns of tableA after left outer join. But i wish to retrieve all the columns from both the tables. Please advice.
Just select both parts into an anonymous type:
var query = from TableA in ds.Tables[0].AsEnumerable()
join TableB in [ ...] on [...] equals [...] into GJ
from sub in GJ.DefaultIfEmpty()
select new { RowA = TableA, RowB = sub };
Each element of the result will have two properties: RowA being a row from TableA, and RowB being a matching row from TableB or null if no rows from TableB matches RowA.