MySQL: how to ignore that column is ambiguous - mysql

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)

Related

Mysql: select value that matches several criteria on multiple rows

Good evening,
I have two tables t1 and t2
In t1, I have two variables, ID (which uniquely identify each row) and DOC (which can be common to several IDs)
In t2, I have three variables, ID (which does not necessarily uniquely identify the rows here), AUTH , and TYPE. Each ID has a maximum of 1 distinct AUTH.
Sample data:
What I would like to do is to select the DOCs that have an ID with AUTH='EP', and that also have an ID with AUTH='US'. They could have additional IDs with other AUTH, but they have to have at least these two.
Thus, i would have a final table with the DOC, ID,and AUTH (there should be at least 2 IDs per doc, but it can be more if there exists an additional AUTH to US and EP for this DOC)
The desired results:
This should work:
SELECT DISTINCT (T1.ID), T1.DOC, T2.AUTH FROM T1
LEFT JOIN T2 ON T2.ID = T1.ID
WHERE T1.DOC IN( SELECT T1.DOC FROM T2
LEFT JOIN T1 ON T1.ID = T2.ID
WHERE T2.AUTH IN('EP','US')
GROUP BY T1.DOC HAVING COUNT(DISTINCT T2.AUTH) = 2) ;
If I could understand correctly the query is going to be something like that:
select t1.doc, t1.id, t2.auth from t1
left join t2 on t2.id = t1.id
where t1.doc in( select t1.doc from t2
left join t1 on t1.id = t2.id
where t2.auth in('EP','US') );
Although, the result set is basically going to be the first sample data table, due to the ID 6 which has a AUTH = "EP" and, consequently, the ID 7 which has the same DOC from ID 6.

Sum the same value with different condition

Ok, the title is cryptic but I don't know how to sintetize it better.
I have a series of expensive similar SELECT SUM queries that must be executed in sequence.
Example:
SELECT SUM(t2.Field)
FROM Table1 AS t1
INNER JOIN (
SELECT Field FROM Table2
WHERE [list of where]
) AS t2 ON ti.ExtKey = t2.Key
WHERE t1.TheValue = 'Orange'
SELECT SUM(t2.Field)
FROM Table1 AS t1
INNER JOIN (
SELECT Field FROM Table2
WHERE [list of where]
) AS t2 ON ti.ExtKey = t2.Key
WHERE t1.TheValue = 'Apple'
And so on.
I've used the nested inner join because after some test it resulted faster than a plain Join.
The rows selected for Table2 are always the same, or at least the same for session.
There's a way to group all the queries in one to speed up the execution?
I was thinking about using a material view, but this would complicate very much the design and maintenance.
I am no sure about your goal. I have a guess for you:
http://sqlfiddle.com/#!9/af66e/2
http://sqlfiddle.com/#!9/af66e/1
SELECT
SUM(IF(t1.TheValue = 'Orange',t2.Field,0)) as oranges,
SUM(IF(t1.TheValue = 'Apple',t2.Field,0)) as apples
FROM Table1 AS t1
INNER JOIN (
SELECT Field, `key` FROM Table2
) AS t2 ON t1.ExtKey = t2.`key`
# GROUP BY t1.extkey uncomment if you need it
If you can provide raw data sample and expected result that would help a lot.
I think you want a group by:
SELECT t1.TheValue, SUM(t2.Field)
FROM Table1 t1 INNER JOIN
(SELECT Field
FROM Table2
WHERE [list of where]
) t2
ON t1.ExtKey = t2.Key
GROUP BY t1.theValue;
Note that your query doesn't quite make sense, because t2 doesn't have a column called key. I assume this is an oversight in the question.
If you want to limit it to particular values, then use a WHERE clause before the GROUP BY:
WHERE t1.TheValue IN ('Apple', 'Orange', 'Pear')

MySQL - is it possible to use aliases for column names in join comparison operators?

Example:
SELECT * FROM T1 JOIN T2 on (T1.id = T2.id as aux)
Where I'm trying to alias T2.id as aux - but that's throwing errors
...
so that the resulting fetched columns would have a column name of aux instead of just id for both o f them.
There's no way to alias columns like this. You could do it in a sub-query, but the proper way to achieve what you want is to explicitly list the projected columns, it's good style anyway. So:
SELECT T1.id, T2.id as aux, T1.col1, T2.col2 [,...] FROM T1 JOIN T2 on T1.id = T2.id

Fetch the rows which have the Max value for a column - with duplicates

This answer from Bill Karwin to question 121387 worked perfectly for me..
"I see many people use subqueries or else vendor-specific features to do this, but I often do this kind of query without subqueries in the following way. It uses plain, standard SQL so it should work in any brand of RDBMS.
SELECT t1.*
FROM mytable AS t1
LEFT OUTER JOIN mytable AS t2
ON (t1.UserId = t2.UserId AND t1."Date" < t2."Date")
WHERE t2.UserId IS NULL;
In other words: fetch the row from t1 where no other row exists with the same UserId and a greater Date."
However, I also need to include in the result a column from a third table (imagine another table with UserId and UserPhoneNumber columns). It feels as if it should be straightforward but it's driving me nuts. Any help would be appreciated.
Simply join the third table after the LEFT JOIN:
SELECT t1.*, t3.*
FROM mytable AS t1
LEFT OUTER JOIN mytable AS t2
ON (t1.UserId = t2.UserId AND t1."Date" < t2."Date")
JOIN myothertable AS t3 ON t1.UserId = t3.UserId
WHERE t2.UserId IS NULL;

LINQ - MULTIPLE TABLES

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