TABLE 1:
TID:
TNAME:
TDESC:
TABLE 2::
AID:
ANAME:
ADESC:
TABLE 3
TID:
AID:
How to write a Linq query to get list of table2 by passing TNAME in TABLE 1.:
Please help!
Assuming you have the correct FK relationships in your database you just type
from t2 in context.table2s
where t2.table3.table1.TNAME == "SomeName"
select t2
Edit
If you don't have foreigns in your database you can either "cheat" by drawing relations in the DBML designer or you need to resort to explicit joining
from t2 in context.table1s
join t3 in context.table3s
on t2.AID equals t3.AID
join t1 in context.table1s
on t3.TID equals t1.TID
where t1.TNAME == "SomeName"
select t2
you need to use join. something like this
from t1 in db.Table1
join t2 in db.Table2 on t1.field equals t2.field
select new { t1.field2, t2.field3}
plz, chek LinqJoin
Related
i am writing a MySQL Stored Procedure which has to join a "base" table with other tables.
Which other table to join depends on a field in the base table.
This is the base table:
ID - Value - JoinValue - TableToJoin - FieldToJoin
---------------------------------------------------
1 - Test - aa - tbl_test1 - test1field
2 - Test2 - bb - tbl_test2 - test2field
As Output, i wanna have:
ID - Value - ValueFromOtherTable
----------------------------------------------
aa - Test - ValueFromTBL_TEST1Field
bb - Test2 - ValueFromTBL_TEST2Field
Is this somehow possible?
Somewhat like this maybe?
SELECT
ID,
Value,
(SELECT #FieldToJoin FROM #TableToJoin AS t WHERE t.ID = #JoinValue) AS ValueFromOtherTable
FROM tbl_base;
Already tried some sort of JOIN, but unfortunately I could not find the answer.
Greetings, xola
This kind of design is usually a bad idea, and you cannot dynamically choose a table to join to in the way you are thinking, but it can be done in a crude rather brute force kind of way....
SELECT stuff
, COALESCE(t2.something, t3.something, ....) AS otherTsomething
FROM t1
LEFT JOIN t2 ON t1.TableToJoin = "t2"
AND t1.JoinValue = CASE t1.FieldToJoin WHEN "a" THEN t2.a WHEN "b" THEN t2.b .... END
LEFT JOIN t3 ON t1.TableToJoin = "t3"
AND t1.JoinValue = CASE t1.FieldToJoin WHEN "a" THEN t3.a WHEN "b" THEN t3.b .... END
;
Usually, the appropriate design for this kind of data is to have t2 and t3 reference t1, then it simply becomes...
SELECT stuff, COALESCE(t2.something, t3.something, ...) AS otherSomething
FROM t1
LEFT JOIN t2 ON t1.id = t2.t1_id
LEFT JOIN t3 ON t1.id = t3.t1_id
;
(For your purposes, this presumes to some degree that only one t2 or t3 will be associated with a t1 record; but that would be hard to enforce, and in general use doesn't really present a problem.)
I have a mysql query like this:
SELECT * from T1
INNER JOIN T2 ON T2.t1_id = T1.id
INNER JOIN T3 ON T3.t1_id = T1.id
where T3.my_column = my_column
I get the Column 'my_column' in where clause is ambiguous error, because all the 3 tables has the my_column column.
What I can NOT do now is to say where T3.my_column = T1.my_column, because I'm using an ORM in which it is impossible now, because I have to setup the condition in the association definition where I do not know if there would be a T1 or T2 table.
The actual value of my_column will be the same in T1 and T2, which means my condition would be ok for both T1.my_column and T2.my_column.
Is there any way in MySQL to say do not care about ambiguous columns, just take one of them randomly ?
What I'd like to do is something like this:
SELECT * from T1
INNER JOIN T2 ON T2.t1_id = T1.id
INNER JOIN T3 ON T3.t1_id = T1.id
where T3.my_column = IGNORE_AMBIGUITY(my_column)
The actual problem
I'm defining an n:m relationship in sequelize, which is an ORM for node.js.
All my tables have an id and a company_id, the id is not unique, only the id-company_id pairs are unique.
This is how I define the association:
{
through: {
model: 'T1_to_T2',
scope: {
company_id: {$col: 'company_id'}, // Problematic part
}
},
foreignKey: 'T2_id'
}
My problem is that through the T1_to_T2 relationship table, for company_id=X1, I'll get the relations of X2 or X3 companies because the query doesn't care about the company_id field in the relationship table.
If you don't know if it would be in T2 or T3, you have a problem. What is your expected behavior? If you are trying to join one or the other then how do you know what you are filtering?
I think you need to rethink your problem first and define what you want from your query before deciding how to solve it. You get the error because MySQL cannot determine what you want.
If you really want one arbitrarily, instead do:
WHERE t3.mycolumn = t1.column OR t3.mycolumn = t2.mycolumn limit 1
That is not random but it is arbitrary.
try this
SELECT * from T1
INNER JOIN T2 ON T2.t1_id = T1.id
INNER JOIN T3 ON T3.t1_id = T1.id
where T3.my_column = (select my_column from T1)
What is difference between these two methods of selecting data from multiple tables. First one does not use JOIN while the second does. Which one is prefered method?
Method 1:
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t2.id
AND t2.id = t3.id
AND t3.id = x
Method 2:
SELECT t1.a, t1.b, t2.c, t2.d, t3.e, t3.f
FROM `table1` t1
JOIN `table2` t2 ON t1.id = t2.id
JOIN `table3` t3 ON t1.id = t3.id
WHERE t1.id = x
For your simple case, they're equivalent. Even though the 'JOIN' keyword is not present in Method #1, it's still doing joins.
However, method #2 offers the flexibility of allowing extra conditions in the JOIN condition that can't be accomplished via WHERE clauses. Such as when you're doing aliased multi-joins against the same table.
select a.id, b.id, c.id
from sometable A
left join othertable as b on a.id=b.a_id and some_condition_in_othertable
left join othertable as c on a.id=c.a_id and other_condition_in_othertable
Putting the two extra conditions in the whereclause would cause the query to return nothing, as both conditions cannot be true at the same time in the where clause, but are possible in the join.
The methods are apparently identical in performance, it's just new vs old syntax.
I don't think there is much of a difference. You could use the EXPLAIN statement to check if MySQL does anything differently. For this trivial example I doubt it matters.
Table 1 has columns : entry_id user_id ...
Table 2 has columns : entry_id user_id ...
the user_id entry is not always the same so I would like to extract both of them so I can later on compare them in my script
SELECT * FROM
table1 as t1
INNER JOIN table2 as t2 on t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'
I would like to extract t1.user_id and t2.user_id ...the problem is the result array has only user_id
thank you
Use AS keyword:
SELECT
t1.user_id as t1_user_id
,t2.user_id as t2_user_id
FROM table1 as t1
INNER JOIN table2 as t2
ON t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'
SELECT t1.user_id AS user_id_1, t2.user_id AS user_id2, ...
I think the framework executing the query is at fault for "merging" the two columns here, the result set should have both, but these are stored into an associative array somewhere along the line.
It is a good idea to only request those columns you actually need nonetheless :)
You can effectively use ALIASING (i.e. use the AS keyword).
SELECT t1.user_id as user_id_1, t2.user_id as user_id_2 FROM
table1 as t1
INNER JOIN table2 as t2 on t1.entry_id=t2.entry_id
WHERE t1.user_id='%s'
Imagine I have table1 which has a column named 'table_name'. I use table1.table_name to store the name of another table in the database. The referenceable tables would all have a field 'target_id.
Is is possible to use table_name in a JOIN statement?
For example:
SELECT t1.*, t2.* FROM table1 AS t1
JOIN table1.table_name AS t2 ON t1.table1_id = t2.target_id
The obvious solution is to use the script (C++ in my case) to get the table name first, and construct a SQL query from it. The question is: can we bypass the script and do this directly in SQL (MySQL)?
Edit: What is dynamic SQL?
The only chance you have is to do 2 SQL statements:
select the tablename you need
use this table-name to dynamically build the secound query to get the data you need - what you want isn't possible to do with SQL directly (and it sounds like you've designed your database wrong in some way - but that's hard to say without knowing what's the goal of it).
I know I'm late to the party, but I wanted to offer a different solution. I see this sort of thing a lot in audit tables. The column table_name would refer to "what table was changed" and table1_id would refer to the ID of the row that changed in that table. In this case, the audit table is pointing back to many different tables that don't normally get joined.
Here goes:
SELECT t1.*, t2.*, t3.*, t4.*, t5.*
FROM table1 AS t1
left JOIN table2 AS t2
ON t1.table1_id = t2.target_id
and t1.table_name = 'table2'
left JOIN table3 AS t3
ON t1.table1_id = t3.target_id
and t1.table_name = 'table3'
left JOIN table4 AS t4
ON t1.table1_id = t4.target_id
and t1.table_name = 'table4'
left JOIN table5 AS t5
ON t1.table1_id = t5.target_id
and t1.table_name = 'table5'
Of course, the main drawback is that each table that can be possibly referenced needs to be explicitly included in the SQL command.
You can get more elegant output using this as your select list:
SELECT
t1.*,
coalesce(t2.fieldA, t3.fieldA, t4.fieldA, t5.fieldA) as fieldA,
coalesce(t2.fieldB, t3.fieldB, t4.fieldB, t5.fieldB) as fieldB
etc