How to Skip particular join from multiple joins in LINQ based external condition - linq-to-sql

Table A,
Table B,
Table C
If the selected screen is A then Join all three tables.
If the selected screen is B then do join on B & C tables.
is it possible to handle the above scenario with a single LINQ query instead of writing multiple LINQ queries based on screen selection?
Example :
from a in A
join b in B on new {} equals new {} join c in C on new {} equals to new {} select{a,b,c}

Related

SQL QUERY - Merge Table

I have a three tables.
Alan1, Alan2, Alan3 are the same in all tables.I want to merge tables. The difference of the tables is the rightmost column. How should I write a SQL Query?
You can join. As commented by Barmar, the idea is to use the first 3 columns as join keys;
select a.*, b.alan4 as alan4b, c.alan4 as alan4c
from a
inner join b
on b.alan1 = a.alan1
and b.alan2 = a.alan2
and b.alan3 = a.alan3
inner join c
on c.alan1 = a.alan1
and c.alan2 = a.alan2
and c.alan3 = a.alan3
This gives you rows that are available in all 3 tables. Say you want to allow "missing" rows in b and/or c, then you need to change the two inner joins to left joins.

Reading multiple fields on one table and joining them single line records from another table

I have two tables A and B. A has unique records while B may have several references to one record in A.
A table -> A.UserID,A.Image1.ID,A.Image2.ID,A.UserName
B table -> B.ImageID,B.ImageURL,ImageDescription
B. Image ID is unique and could have at least two records correspond to Image!ID and Image2ID in table A.
In my query, need to read A.UserName,B.Image1URL and B.Image2URL.
Following SQL query is to read one image. How I could modify this to read both Image1 and Image2 in one SQL query ?.
#"SELECT A.*,B.* FROM A
INNER JOIN B ON B.Image1ID = A.Image1ID
WHERE A.UserID = #Parameter1;";
So in the result, I need following :
UserID
Image1URL
Image2URL
What's the best way to get this done in mySQL ?
You can join same table twice -
SELECT A.UserID, A.UserName, img1.ImageURL, img2.ImageURL
FROM A
INNER JOIN B as img1
ON img1.ImageID = A.Image1ID
INNER JOIN B as img2
ON img2.ImageID = A.Image2ID
WHERE A.UserID = #Parameter1;

Combining mysql statements from different table depending on the column value

I need help on how best to handle this situation.
I have 3 tables, say A, B, C.
Table A has a column categoryID with either G (Good) or B (Bad).
If categoryID in A is G, the information about that row is in table B.
If categoryID is B, the information about that row is in C.
For example if i have 2 records in table A.
For the first record, categoryID is G. I need to get the data from table B for the first record.
For the second record, categoryID is B, I need to get the data from table C for the second record.
I'm using MySQL procedure. I've tried to use case, but doesn't seem to work.
I want to achieve this in a single procedure. Tables B and C have foreign key referenced column from table A.
One approach is two left joins. Your question is a bit vague on what the column names really are, but the approach is something like this:
select a.*, coalesce(b.col1, c.col1) as col1, coalesce(b.col2, c.col2) as col2
from a left join
b
on a.categoryId = 'G' and a.bid = b.bid left join
c
on a.categoryid = 'B' and a.cid = c.cid;

sql query to take data out of multiple tables

I have (several tables say) 4 tables, A, B, C and D and they are linked by some common values like:
TableA(A1,A2,A3,A4) ---> TableB(A1,B2,B3,B4) ---> TableC(C1,C2,B3,C4) ---> TableD(D1,D2,D3,C4)
| |
TableX(X1,B2,X3,X4) TableY(Y1,Y2,Y3,C4)
For each unique value of A2, there is a value A1. Same Value A1 is there in TableB.
Similarly in TableB, for each value of A1, there are some particular values B2,B3,B4,
In the same way in TableC and TableD also, for each value of B3 and C4 respectively there are particular remaining values.
In addition to the linear flow there are multiple flows inbetween (like from TableB to TableX or TableC to TableY)
I want to extract some columns say for instance A1,A2,B2,C1,D1 and C4 if I am given a value of A2.
I am new to this type of queries and short of time to complete the task.
Thanks in advance for the help.
You must use a multiple join like this:
Select TableA.A1,TableA.A2,TableB.B2,TableC.C1,TableC.C4 from
TableA join TableB on TableA.A1=TableB.A1
join TableC on TableB.B3=TableC.B3
where A2='....'
Although a horrible question posted with poor sample of tables and explanations, Start first with a query on how to tie all the fields together. Once you have all your JOIN conditions established, THEN add the WHERE you are looking for specific elements from the data
select *
from
TableA a
JOIN TableB b
on a.A1 = b.A1
JOIN TableC c
on b.b3 = c.b3
JOIN TableD d
on c.c4 = d.c4
where
a.field = '?'
AND d.field = '?'
or whatever criteria you are looking for... and just use the alias references, such as (a, b, c, d)
Notice from the layout I have my SQL, you can exactly see the hierarchy of how one table connects to the other by simple visual indentation and very clear column relations between the respective tables.

ebean fetch with multiple oneToMany relationship

I have a play! framework project that uses ebean for models management. I have 4 models, A B C D. A and B is oneToMany, B and C is OneToMany, B and D is One To Many. Now I want a list of A that is linked with all related B C D.
What I have now is
A.find.fetch("Bs", new FetchConfig().query()).fetch("Bs.Cs", "Bs.Ds", new FetchConfig().query()).findList();
But the performance is really poor and according to the sql log the sql queries are not joined as I want.
Is there any way that I can use query join for all of them? (i.e. select all A B C D with 4 queries and join them locally instead of separate queries for each B?)
javadoc for new FetchConfig.query()
...
Eagerly fetch the beans in this path as a separate query (rather than as part of the main query).
That is, you should remove the FetchConfig.query() if you want to use a SQL join rather than a separate query.
That said, Ebean will not return A B C D in a single query as that would result in a cartesian product and it will automatically break up the query to avoid that.