how can I convert this query to be compatible with the find () function in cakephp
query : select role from table1 , table2
Where table1.id = table2.id and table1.name = 'ABC'
You can use $this->Model->query($yourQuery)
Related
I have a piece of code on plpgsql like that:
UPDATE table1
SET column1 = value1
FROM table1 tbl
INNER JOIN table2 tbl2 ON tbl2.id = tbl.id
WHERE table2.code = 200
AND table1.id = tbl.id;
Here table1 and alias tbl are two different table due to postgress specification.
Now I want to write the same procedure for SQL Server, but this code fails with error
[S0001][4104] The multi-part identifier "table1.id" could not be bound.
How should I rewrite this code for SQL Server?
This is simpler in SQL Server, because the UPDATE can reference an alias in the FROM. There is no need to JOIN back to the original table.
So:
UPDATE tbl
SET column1 = value1
FROM table1 tbl JOIN
table2 tbl2
ON tbl2.id = tbl.id
WHERE tbl2.code = 200;
This assumes that id is the primary key of table1 (a reasonable assumption).
You could use common table expression:
WITH cte AS (
SELECT tbl.column1
FROM table1 tbl
JOIN table2 tbl2 ON tbl2.id = tbl.id
WHERE tbl2.code = 200
)
UPDATE cte SET column1 = value1;
-- SELECT * FROM cte
By highlighting and running only select statement inside cte you could check the result before doing actual update.
db<>fiddle demo
SELECT name FROM table1
WHERE name NOT IN (
SELECT name, school FROM table2
UNION
SELECT name, school FROM table3
)
This syntax keeps flagging error near syntax 'Union'. Any suggestion on how to fix this please?
In the subquery, only select the name column that's used as a filter. Unless you need combination of the name and school columns, in which case they'll need to be concatenated. The space in the name of table3 will need to be removed, unless this is the actual name of the table. If it is, it will be to be enclosed in brackets, i.e. [table 3].
SELECT name FROM table1
WHERE name NOT IN (
SELECT name FROM table2
UNION
SELECT name FROM table3
)
This is functionally the same and may be faster in some cases
SELECT name
FROM table1 as t1
left join table2 as t2 on t1.name = t2.name
left join table3 as t3 on t1.name = t3.name
where coalesce(t2.name,t3.name,'new') = 'new'
you can also use a where clause like this:
where t2.name is null and t3.name is null
I am writing a simple select subquery statement and it is not giving any result. Neither it is throwing any error.
My Sql query is like this -
select * from Table1
where id in (select ID from Table2 where user = 'xyz')
I tried with exists also, but not showing any result.
Appreciate your help.
It could be that the below subquery returns a empty list because the filter condition where user = 'xyz' doesn't matches any record
select ID from Table2 where user = 'xyz'
The outer query as below doesn't matches the condition where id in ()
select * from Table1 where id in ()
Thus returning an empty result set.
You can convert your posted query to be a INNER JOIN query like
select t1.*
from Table1 t1 join Table2 t2
on t1.id = t2.id
where t2.user = 'xyz';
As a test, try using LIKE operator instead of equality comparison
select * from Table1
where id in (select ID from Table2 where user like '%xyz%')
I have an sql query like this
Select col1, (select abc from table2 where def=1) as col2
From Table1 inner join table3 on Table1.id = table3.id
Where col2 = 4
The problem is that the where condition doesn't work. I get an error saying
Invalid column name 'col2'
Kindly help me fix this sql query.
Thanks in advance
You can define it in a CROSS APPLY and then reference in the SELECT and WHERE
SELECT col1,
col2
FROM Table1
INNER JOIN table3
ON Table1.id = table3.id
CROSS APPLY (SELECT abc
FROM table2
WHERE def = 1) C(col2)
WHERE col2 = 4
Using a CTE (Common Table Expression):
WITH SubQuery AS (Col2) {
SELECT
ABC
FROM
table2
WHERE
def = 1
}
SELECT
T.Col1,
S.Col2
FROM
SubQuery S,
Table1 T
INNER JOIN table3 t3
ON T.id = t3.id
WHERE
S.Col2 = 4
Although I must say I agree with the first comment - this makes no sense since your subquery is not correlated (joined) to the rest of your query...
I'm trying write a routine in SQL Server that, when run, would traverse specified tables and, if a specific column contains a value, update another value. In pseudo code:
select * from table1
if column1 = true
{
update table2.column1 with value where table2.column2.value = table1.column2.value
}
Basically, traverse table1 and if the value in a specific column is true, update the value of another table's column1 where that row's column2 matches table1's column2
Thanks
You don't need the IF, just use a WHERE clause:
UPDATE T2
SET t2.Column1 = 'blah'
FROM Table2 t2
INNER JOIN Table1 t1
ON t1.value = t2.value
WHERE t1.column1 = 'True'
Not sure I fully understand your requirement, but if you want update all the values using a single update statement this might do:
update table2 set column1 =
(select column1 from table1 t1
where t1.column2 = table2.column2
)
where exists
(select * from table1 t1
where t1.column2 = table2.column2 and t1.column1 = true
)
It could be written to avoid having the 2nd subselect (the EXISTS clause) but the solution is rather dialect-specific. This one is, I believe, more likely to be accepted in an unknown dialect. (Except for the word "true" ... substitute the value you want, there. )