join another table without a join for limit at one - mysql

SELECT op.id,op.nome,op.cognome,op.ore_giornaliere,
(select tp.* from turni_preconf as tp where tp.tot_ore = op.ore_giornaliere limit 5,1)
FROM operatori as op
return me an error :
1241 - Operand should contain 1 column(s)
i need o select an other table without join
Thank you

Your subquery can return only one column and one row (i.e. one value) when used as a column expression. If you need more than one column from the table in the subquery, you will need more than one subquery. E.g.:
SELECT t1.a, t1.b
(SELECT TOP 1 t2.a FROM t2 WHERE something = true) as c,
(SELECT TOP 1 t2.b FROM t2 WHERE something = true) as d
FROM t1
Here is some good reading on subqueries: Subquery Fundamentals

Related

How do I return 0 when my sql returns with no rows?

select a, count (b)
from table1 where b in ( select distict b from table2)
and table1.dated>=DATE('yy/mm/dd')
group by a;
In the above SQL, when I have count(b)>0 then it returns columns but when count=0 then no rows were returned
I did try UNION, NULLIF() and SELECT(SELECT()) as something but nothing worked.
I was expecting to get 0 returned if the count is equal to 0.
https://www.db-fiddle.com/#&togetherjs=2AkxeMUrPF
You could use:
select table1.a, count(DISTINCT table2.b)
from table1
LEFT JOIN table2
ON table1.b = table2.b
AND table1.dated>=DATE('yy/mm/dd') -- this comparision is simply incorrect
group by table1.a
We can ensure that a query returns a row by having the query guarantee it.
Here's an example that retrieves exactly one row from an inline view i.
Then an outer join to another inline view s that gets a distinct list of values.
And then and then another outer join to table1.
SELECT t.a
, COUNT(t.b) AS cnt
FROM ( SELECT 1 AS n ) i
LEFT
JOIN ( SELECT DISTINCT r.b
FROM table2 r
) s
LEFT
JOIN table1 t
ON t.b = s.b
AND t.dated >= ...
GROUP
BY i.n
, t.a
If inline view s returns no rows, the query should return
a cnt
---- ---
NULL 0

MySQL IN clause EITHER value

I have this query:
SELECT 1 IN (1,2)
Which returns 1 since 1 is inside (1,2).
What I want to do is check if either one of two values exists in the array. In an imaginary world:
SELECT (3,1) EITHER IN (1,2)
Something like this should return 1 since at least one value was found in the second array. Of course this query is incorrect. Is there a way to do this and avoid this:
SELECT (
3 IN (1,2)
OR
1 IN (1,2)
)
you can use an inner join for that
select a, b form T1
inner join T2 on (T1.a = T2.c or T1.b = T2.c)

MySQL WHERE EXISTS evaluating to true for all records

I'm trying to run a query that retrives all records in a table that exists in a subquery.
However, it is returning all records insteal of just the ones that I am expecting.
Here is the query:
SELECT DISTINCT x FROM T1 WHERE EXISTS
(SELECT * FROM T1 NATURAL JOIN T2 WHERE T2.y >= 3.0);
I've tried testing the subquery and it returns the correct number of records that meet my constraint.
But when I run the entire query it returns records that should not exists in the subquery.
Why is EXISTS evaluating true for all the records in T1?
You need a correlated subquery, not a join in the subquery. It is unclear what the right correlation clause is, but something like this:
SELECT DISTINCT x
FROM T1
WHERE EXISTS (SELECT 1 FROM T2 WHERE T2.COL = T1.COL AND T2.y >= 3.0);
Your query has a regular subquery. Whenever it returns at least one row, then the exists is true. So, there must be at least one matching row. This version "logically" runs the subquery for each row in the outer T1.
Q: Why is EXISTS evaluating true for all the records in T1?
A: Because the subquery returns a row, entirely independent of anything in the outer query.
The EXISTS predicate is simply checking whether the subquery is returning a row or not, and returning a boolean TRUE or FALSE.
You'd get the same result with:
SELECT DISTINCT x FROM T1 WHERE EXISTS (SELECT 1)
(The only difference would be if that subquery didn't return at least one row, then you'd get no rows returned in the outer query.)
There's no correlation between the rows returned by the subquery and the rows in the outer query.
I expect that there's another question you want to ask. And the answer to that really depends on what result set you are wanting to return.
If you are wanting to return rows from T1 that have some "matching" row in T2, you could use either a NOT EXISTS (correlated subquery)
Or, you could also use a join operation to return an equivalent result, for example:
SELECT DISTINCT T1.x
FROM T1
NATURAL
JOIN T2
WHERE T2.y >= 3.0
It isn't working because there is no correlation between the outer query and the subquery being used. Below there is a correlation in the form of and T1.id = T2.id
SELECT DISTINCT x
FROM T1
WHERE EXISTS ( SELECT 1 FROM T2 WHERE T2.y >= 3.0 and T1.id = T2.id)
;
But, without knowing the data I'd hope you do NOT need to use "distinct" in that query, and this would produce the same result:
SELECT x
FROM T1
WHERE EXISTS ( SELECT 1 FROM T2 WHERE T2.y >= 3.0 and T1.id = T2.id)
;
An alternative, which probably would require distinct, is a variation ofh the second half of your second query
SELECT DISTINCT x FROM T1 NATURAL JOIN T2 WHERE T2.y >= 3.0
You can use an INNER JOIN to get where you're trying to go:
SELECT DISTINCT T1.X
FROM T1
INNER JOIN T2
ON T2.COL = T1.COL
WHERE T2.Y > 3.0
Share and enjoy.

Compare 2 tables and find non duplicate entries

I have 2 tables. I want to check if columns of table 1 don't have duplicates in columns of table2.
Here is how the search should work!
If no duplicates are found, I want to get the row name from table1.
If I got you right, this is what you want.
SELECT
t1.name
FROM
Table1 t1
WHERE
t1.name
NOT IN
(
SELECT t2.name
FROM Table2 t2
JOIN t1
ON t2.name = t1.name
)
You need to specify a column (or columns) that you will use to "match" the rows, to determine whether they are "duplicates".
I'm going to assume (absent any schema information), that the column name is id.
An "anti-join" pattern is usually the best performing option:
SELECT a.id
FROM table1 a
LEFT
JOIN table2 b
ON a.id = b.id
WHERE b.id IS NULL
(Performance is dependent on a whole bunch of factors.)
Your other options are to use a NOT EXISTS predicate:
SELECT a.id
FROM table1 a
WHERE NOT EXISTS
( SELECT 1
FROM table2 b
WHERE b.id = a.id
)
Or, use a NOT IN predicate:
SELECT a.id
FROM table1 a
WHERE a.id NOT IN
( SELECT b.id
FROM table2 b
WHERE b.id IS NOT NULL
)
The generated execution plan and performance of each of these statements will likely differ. With large sets, the "anti-join" pattern (the first query) usually performs best.

How to fetch two different column values from two different tables inturn one value table is dependent on other table value

The following query is the one I was using previously... but I want to combine these two queries in order to improve the performance
select a, b, c
from table1
where d LIKE 'xxx'
and f like 'yyyy'
order by b desc;
I'm executing the above query and reading values.
For every value of b from above query again executing the below query in a loop.
select count(*)
from table2 where b=? AND js_email_id IN
(
select js_email_id
from js_initialsignup
where UCase(jsaccountstatus) LIKE UCase('live')
AND UCase(js_status) LIKEUCase('accepted')
)
How can I combine both queries and get count and values at a time?
select a,b,c,
(select count(*)
from table2 where b=a.b AND js_email_id IN
(
select js_email_id
from js_initialsignup
where UCase(jsaccountstatus) LIKE UCase('live')
AND UCase(js_status) LIKEUCase('accepted')
)) as cnt
from table1 a
Try this:
SELECT t1.a, t1.b, t1.c, COUNT(t2.*)
FROM table1 t1
INNER JOIN table2 t2 ON t1.b = t2.b
INNER JOIN js_initialsignup j ON t2.js_email_id = j.js_email_id
WHERE t1.d LIKE 'xxx'
AND t1.f like 'yyyy'
AND UCase(j.jsaccountstatus) LIKE UCase('live')
AND UCase(j.js_status) LIKE UCase('accepted'))"
GROUP BY t1.a, t1.b, t1.c
ORDER BY by t1.b DESC;