I have two tables which as same column name called id (stores record id). I'm using an inner join on these 2 tables and taking the id column of 2 tables. I'm using this select query in the angularjs http.get rest API. When the rest API throws the resultant table which contain 2 column of same name-id, I'm storing this in scope variable.
The problem is how can i use the id of one particular table.
consider query is,
select t1.id,t2.id
from table1 AS t1
INNER JOIN table2 AS t2 ON t2.product=t1.product_id
I am storing the result of rest API in scope variable,
$scope.details=response.data.platform.record;
I used these below syntax inside alert box in controller but not working,
$scope.details.id, $scope.details.t1.id.
Using column alias name, will solve your problem:
SELECT t1.id AS T1Id,
t2.id AS T2Id
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t2.product = t1.product_id
and in your controller, you can use as:
$scope.details.T1Id
$scope.details.T2Id
Related
Update details
set details.a=a, details.b=b
where Id=10 select a,b from #userdefinedtabletype
Here id is unique for all the records
Since you have not provided much detail as to what either table has in it I am going to assume that your user defined table only has one row in it. Otherwise you will get the last values that are returned from the query.
So I would use a CROSS APPLY to do the update such as:
Update details
set details.a=t2.a, details.b=t2.b
from details t1
CROSS APPLY (select a,b from #userdefinedtabletype) t2
where t1.Id=10
If in fact these tables are related by an ID of some sort then you would probably want to do an INNER JOIN:
Update details
set details.a=t2.a, details.b=t2.b
from details t1
INNER JOIN #userdefinedtabletype t2 on t1.id = t2.id
where t1.Id=10
I need to display 3 columns from table A and one column from table B. I used the following join query to combine them but not getting the value in the expected columns.
example1 -this is working.(If I display one column from table A and one column from table B it works)
SELECT
tableA.col1(fkid),
tableB.col1
FROM tableA
INNER JOIN tableB
ON tableA.fkid = tableB.pkid;
I need to display 3 columns from table A and one column from table B the below query is not working.
SELECT
tableA.col1,
tableA.col2,
tableA.col3,
tableB.col1
FROM tableA
FULL JOIN tableB
ON tableA.fkid = tableB.pkid;
Original query:
select device.name,device.description, device.fkphonetemplate,phonetemplate.name from device inner join phonetemplate ON device.fkphonetemplate=phonetemplate.pkid;
Result:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 ARRAY(0xc7153b0)
Expected result should be:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 Standard 7960 SCCP
I don't get name from device table in the result and the name from phonetemplate table shows something as Array0X... but I need to get the name of phonetemplate like Standard 7960 as shown in expected result.
Can you refine my query or suggest what's wrong with the 2nd query?
Any reason for you to using full join instead of inner join ?
If no,try using inner join.
SELECT
a.col1,
a.col2,
a.col3,
b.col1
FROM tableA a
INNER JOIN tableB b
ON a.fkid = b.pkid;
Can you provide your database sample data , so we can clearly know what your need.
The above table has recursive relationship ie. the crfor column has reference to resourceid column.
So the number 1 in the crfor column means resourceid = 1 .How should I change my query to make crforname column (dynamic column) display the proper name Arjun instead of Ashok?
I used the query below to get the result above. You can ignore the other table call PCR in the query
select t1.*,t2.pcrname,t1.ResourceName AS crforName from
Resource t1 left join Pcr t2 on t1.Pcrid=t2.pcrid
where t1.contractid=1
Try adding a self join on the Resource table joining the ResourceId to the CrFor:
select t1.*,t2.pcrname,t3.ResourceName AS crforName from
Resource t1 left join Pcr t2 on t1.Pcrid=t2.pcrid left join Resource t3 on t1.CrFor = t3.ResourceId
where t1.contractid=1
I have two tables, that we'll call t1 and t2. I want to select the data in t1 that has a certain ID that I can only find using a where clause in t2. I don't want to select the data in t2 (many duplicate column names with different data) so how do I do that?
try this
select * from t1 where t1.Id in (select distinct Id from t2)
Another approach is to join the tables
SELECT * FROM t1
JOIN t2 on t1.id = t2.id
You are joining them on a specific ID common between the 2 tables.
I'm trying to update some rows in a table joined to another table. The left table is indexed by ID; the right table is joined on the same ID but has multiple rows per ID. My query looks like:
UPDATE t1
SET t1.modified = t2.created
FROM table1 t1
INNER JOIN table2 t2
ON t1.ID = t2.ID
Note that t1.modified and t2.created are both datetime.
As stated, t2 has several rows per ID, each with a different created value (so the primary key is t2, created). What I want to do is set the max value of t2.created=t1.modified. However, when joining, the t1.modified value gets updated in no particular order, so whichever row is updated last, that's the value that t1.modified gets. I tried using t1.modified=max(t2.created), but apparently I can't use aggregate functions in an update query, nor can I use an order clause (i.e. order the rows so that the last row updated will effectively be the latest value of t2.created) in the update query.
Any help you can provide me is much appreciated! Thanks!!
how about this? will this fit your need?
UPDATE t1
SET modified = isnull((SELECT max(t2.created)
FROM table2 t2
WHERE t2.ID = t1.ID), modified)
FROM table1 t1;
Use the isnull function to set modified to be itself if the value returned is null. that should take care of the null issue.
You could do it this way, although it may not be standard SQL. However it should work for SQL Server.
update t1
set t1.modified = (select max([created]) from t2 where t1.ID = t2.id)