Selecting columns from two tables Yii2 - yii2

I have a join query between tables(t1,t2) that needs to select some columns from BOTH tables .
Doing something like :
t1::find()->select("t1.id , t2.id")->innerJoin('t2 ...)
but, it doesn't retrieve t2.id .
How to do it in Yii ?
The corresponding MySql query :
select t1.id , t2.id from t1 inner join t2 on t1.id = t2.id;

If you're using an ActiveRecord you either need the id as a property on the AR or use asArray.
e.g.
t1::find()->select("t1.id , t2.id")->innerJoin('t2 ...')->asArray()->...

Related

Create view by selecting different columns from multiple tables in SQL server

Im trying to create a view where specific Columns from 2 tables to be combined.
My Tables
Table1(Column1,Column2,Column3)
Table2(Column4,Column5,Column6)
Expected output
View1(Column2,Column3,Column6)
What query can i use to achieve that output?
CREATE VIEW [dbo].[View]
AS
SELECT a.Column2,a.Column3,b.Column6
FROM Table1 AS a
INNER JOIN Table2 AS b ON A.ID = B.ID -- your logic
If you have and id to join tables:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
join table2 t2 on t1.id = t2.id
If you don't have and id and want to get mixes columns without relationships:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
cross join table2 t2 on t1.id = t2.id
As Fourat and jarhl commented above, you need to explicitly define what columns are used to JOIN both tables. So using the below code change the ID column to the column that relates these two tables, and if it's more than one column the list them all separated by AND.
CREATE VIEW dbo.YourViewName
AS
SELECT t1.Column2, t1.Column3, t2.Column6
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
GO

MySQL: how to ignore that column is ambiguous

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)

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

MYSQL Select table and column name

I've got a SQL query like this one :
SELECT T1.id, T2.user
FROM T1
INNER JOIN T2 ON T1.user_id = T2.id
The result is something like id | user.
I'd like to know if there's a way to have the same result with the table name before the column name (something like T1.id | T2.user) without changing the query (with a concat for example) ?
Thanks !
EDIT : So I've found an answer to my question. I'm using PHP function mysqli_result::fetch_field_direct which allows me to get metadata for each fields.
Thank you guys for all your answers !
You know the tables you're using, then you can add their name in your query :
SELECT
CONCAT('T1.', T1.id) as id,
CONCAT('T2.', T2.user) as user,
FROM T1
INNER JOIN T2 ON T1.user_id = T2.id
As far as I understood your question, you need to use an alias:
Select t1.id as UserID, t2.user as Username
From t1 inner join t2 on t1.user_id = t2.id

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