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
Related
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()->...
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
I wrote a query in this format..
Select * INTO #xyz FROM ()
which I later want to use to create a view, as...
CREATE VIEW aaa
AS
Select * INTO #xyz FROM ()
but getting the following errors:
Incorrect syntax near the keyword 'INTO'.
Views or functions are not allowed on temporary tables
Can someone please suggest a workaround this? Is it possible to use temporary tables to create views?
You certainly cannot use a select into in a view. And a termp table is not approraite either. Use a derived table that is the equivalent of the temp table instead. Example:
Suppose you had:
select t1.id, t2.field1 into #temp from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest'
select t1.id, t2.field1, t1.field1
from mytable2 t1
join #Temp t2 on t1.id = t2.id
where t2.somefield = 'test'
Then you could use:
select t1.id, t2.field1, t1.field1
from mytable2 t1
join (select t1.id, t2.field1
from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest') t2
on t1.id = t2.id
where t2.somefield = 'test'
You could also usea a CTE
Just as the error message says it is not possible to use temp tables.
You should use a permanent table or a CTE which can also be specified in a view.
A CTE could help you out depending on your situation. Describe your problem with some context if you think it suitable after researching what a CTE is. In short a CTE is a query that you can reference multiple times which in the past people used temp tables for.
http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx
First a quick explanation: I am actually dealing with four tables and mining data from different places but my problem comes down to this seemingly simple concept and yes I am very new to this...
I have two tables (one and two) that both have ID columns in them. I want to query only the ID columns that are in table two only, not in both. As in..
Select ID
From dbo.one, dbo.two
Where dbo.two != dbo.one
I actually thought this would work but I'm getting odd results. Can anyone help?
SELECT t2.ID
FROM dbo.two t2
WHERE NOT EXISTS(SELECT NULL
FROM dbo.one t1
WHERE t2.ID = t1.ID)
This could also be done with a LEFT JOIN:
SELECT t2.ID
FROM dbo.two t2
LEFT JOIN dbo.one t1
ON t2.ID = t1.ID
WHERE t1.ID IS NULL
Completing the other 2 options after Joe's answer...
SELECT id
FROM dbo.two
EXCEPT
SELECT id
FROM dbo.one
SELECT t2.ID
FROM dbo.two t2
WHERE t2.ID NOT IN (SELECT t1.ID FROM dbo.one t1)
Note: LEFT JOIN will be slower than the other three, which should all give the same plan.
That's because LEFT JOIN is a join followed by a filter, the other 3 are semi-join
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'