I have to call a function into a select, and at the same time, i have to do a select into that function which is matching with the biggest select.
I try this but it doesn't work:
SELECT field1,
field2,
function(select field3 from table2 where table2.id = table1.id and table2.id = 3)
FROM table1
WHERE ...
How should i do the select into the function?
You should do a Union of results as
SELECT field1,
field2,
FROM table1
WHERE ...
Union All
Select function(select * from table2 where table2.id = table1.id) --function returning max
Related
I have this SQL query
SELECT table1.*
FROM table1 table1
WHERE table1.table2_id IN (SELECT table2.id
FROM table2
WHERE table2.locked = 0)
I get the result and it works fine, but now I want to count how many rows exist.
I tried something like this:
SELECT table1.count(*)
FROM table1 table1
WHERE table1.table2_id IN (SELECT table2.id
FROM table2
WHERE table2.locked = 0)
But nothing worked…
How can I count the rows in this kind of query?
Try this
SELECT COUNT(*)
FROM table1 table1
WHERE table1.`table2_id` IN (SELECT table2.id FROM table2 WHERE table2.locked = 0)
Hope it will help you! ..
I wanted to know if there is a way, in SQL Oracle, to do some range-definition (like in Excel). For example:
DEFINE TABLE1 = SELECT FIELD1, FIELD2, FIELD3 FROM [SCHEMA].[TABLE0][WHERE/GROUP BY/HAVING/ORDER BY/...];
DEFINE TABLE2 = SELECT FIELD1, FIELD2, FIELD3 FROM TABLE1 [WHERE/GROUP BY/HAVING/ORDER BY/...];
DEFINE TABLE3 = SELECT FIELD1, FIELD2, FIELD3 FROM TABLE2 LEFT JOIN TABLE1 ON [CONDITIONS];
SELECT * FROM TABLE3;
Thanks a lot in advance.
Based on your examples, it sounds like you want to create views:
CREATE VIEW TABLE1 AS
SELECT FIELD1, FIELD2, FIELD3
FROM [SCHEMA].[TABLE0][WHERE/GROUP BY/HAVING/...];
CREATE VIEW TABLE2 AS
SELECT FIELD1, FIELD2, FIELD3
FROM TABLE1 [WHERE/GROUP BY/HAVING/...];
CREATE VIEW TABLE3 AS
SELECT FIELD1, FIELD2, FIELD3
FROM TABLE2
LEFT JOIN TABLE1 ON [CONDITIONS];
SELECT * FROM TABLE3;
TO close this question. From one of the comments (Steve), what I needed is a WITH clause, as I didn't have DDL privileges.
Thanks,
I'm trying to return all columns in a database where certain rows within certain columns have been eliminate. Is there any possible way to do this? I tried using code like this but I'm unsure what I am missing to make this work
Select * from table1
where (select column1 from table1
minus select column1 from table2);
You can do this with a WHERE NOT EXISTS:
Select T1.*
From Table1 T1
Where Not Exists
(
Select *
From Table2 T2
Where T2.Column1 = T1.Column1
)
Alternatively, you could use a LEFT OUTER JOIN:
Select T1.*
From Table1 T1
Left Join Table2 T2 On T2.Column1 = T1.Column1
Where T2.Column1 Is Null
Or even a WHERE NOT IN:
Select *
From Table1
Where Column1 Not In
(
Select Column1
From Table2
)
I would recommend the WHERE NOT EXISTS approach, but to fix the query you have in the question, you just need to add a WHERE IN:
Select *
From Table1
Where Column1 In
(
Select Column1
From Table1
Minus
Select Column1
From Table2
)
Try this:
select * from table1 where column1 in
(select column1 from table1
minus
select column1 from table2);
I have the following query (simplified) on MS Access:
SELECT * FROM table1 WHERE table1.ID NOT IN (SELECT DISTINCT table1id FROM table2);
My problem is it doesn't work, but these two ones work:
SELECT * FROM table1 WHERE table1.ID IN (SELECT DISTINCT table1id FROM table2);
SELECT * FROM table1 WHERE table1.ID NOT IN (2, 3);
The first one simply returns me an empty set, while I know I have records on table1 with ids ranging from 1 to 9, and only 2 and 3 are use on table 2.
Any help?
Generally, the problem with IN and NOT in has to do with NULLs in the subselect. Try this and see if it works:
SELECT *
FROM table1
WHERE table1.ID NOT IN (SELECT DISTINCT table1id FROM table2 where tableid is not null);
I would like to expand on this simple sub select:
Select * from table1 where pkid in (select fkid from table2 where clause...)
The logic above is fairly simple - get me all rows in table1 where the pkid is contained in the subset returned from the sub select query that has a where clause. It works well because there is only 1 field being returned.
Now I want to expand on this.
In table 1 I want to return results where field1 and field2 and field3 in select (field1, field2, field3 from table2 where clause...)
How is this possible?
Thanks in advance.
Example.
TABLE1
FIELD1 FIELD2 FIELD3
1 2 3
2 3 4
4 5 6
TABLE 2
2 3 4
4 5 6
I want to return 2 results.
If I understand what you need you can try:
SELECT t1.field1, t1.field2, t1.field3 FROM table1 t1
INNER JOIN table2 t2
ON t1.field1 = t2.field1
AND t1.field2 = t2.field2
AND t1.field3 = t2.field3
AND t2.... // Use this as WHERE condition
Like Marco pointed out, what you want to do is an INNER JOIN.
But (that's just FYI, you should definitely use Marco's solution) it's also possible to simply use braces.
Select *
from table1
where (field1, field2, field3) in (select field1, field2, field3 from table2 where clause...)
At least in MySQL (wasn't this question tagged with MySQL?)
you can use a temporary table
select field1, field2, field3 into #tempTable from table2 where clause...
select * from table 1
where filed1 in (select field1 from #tempTable)
and filed2 in (select field2 from #tempTable)
and filed3 in (select field3 from #tempTable)
Avoid using IN for most cases like this. It's very limitting.
I prefer to use a JOIN in most cases.
SELECT
*
FROM
yourTable
INNER JOIN
(SELECT c1, c2, c3 FROM anotherQuery) AS filter
ON yourTable.c1 = filter.c1
AND yourTable.c2 = filter.c2
AND yourTable.c3 = filter.c3
(Ensure the filter returns unique combinations of c1, c2, c3 using DISTINCT or GROUP BY if necessary)
you didn't mentioned engine, so I'll assume SQL Server.
This query will show you what's on both tables
select FIELD1, FIELD2 from table1
intersect
select FIELD1, FIELD2 from table2