MySQL IN clause EITHER value - mysql

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)

Related

To show the aggregate of duplicated values that another corresponding attribute in another table is NOT NULL

Not sure if I have described the context clearly in the title
but the situation is given two tables
A|B
1|1
1|2
2|3
2|4
3|5
4|6
5|7
5|8
B|C
1|NULL
2|1
3|NULL
4|NULL
5|NULL
6|2
7|3
8|4
the condition of output is the value of A should appear more than 1 time,
while the corresponding B values are not both NULL in C (at least 1 value of C from B is not NULL)
what matches the conditions of the above table by A should be 1 and 5
the expected output of the count of duplicated A is 2
The following should help.
select t.a,count(t.a),count(t2.c)
from t
join t2
on t.b=t2.b
group by t.a
having count(t.a)>1
and count(t2.c)>=1
Here i join the table t and t2 on the column b
after that i check how many records have col-a in table t are >1 and also how many in col c in t2 are> 1.
Just FYI,count(null) would be zero) so any non-null value in t2.c will have count(t2.c)>=1
Full demo
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=6d56b0ed8bafe09786342a6bfb58b8d2
you can try by using exists
select t1.A,count(*) as duplicate
from tableA t1
where exists ( select 1 from tableB t2 where t2.B=t1.B
and COALESCE(t2.B,t2.c) is not null
)
group by t1.A
having count(*)>1

join another table without a join for limit at one

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

Selecting two rows in a table which have the same data for a particular column

There is a column in a table(contracts) called service location. I have to show all the rows where the service locations matches any other row in the table.
Table Example
A B C
1 2 3
3 2 1
2 5 3
I require a query where the first and second rows will be returned based on a comparison on the second column. I am assuming I will need to use a HAVING COUNT(B) > 1
I came up with this
SELECT `contract_number`
FROM `contracts`
WHERE `import_id` = 'fe508764-54a9-41f7-b36e-50ebfd95971b'
GROUP BY `service_location_id`
HAVING COUNT(`service_location_id` ) >1
But it doesn't generate what I exactly need.
Having would do it, but you would need to use it like this
SELECT *
FROM Contracts
INNER JOIN
( SELECT B
FROM Contracts
GROUP BY B
HAVING COUNT(*) > 1 -- MORE THAN ONE ROW WITH THE SAME VALUE
) dupe
ON dupe.B = Contracts.B
Depending in your indexing you may find a self join performs better though:
SELECT DISTINCT t1.*
FROM contracts t1
INNER JOIN contract` t2
ON t1.B = t2.B
AND t1.A <> t2.A
SELECT *
FROM sheet1
WHERE C
IN (
SELECT C
FROM sheet1
GROUP BY C
HAVING COUNT( C ) >1
)
ORDER BY C
LIMIT 0 , 5000

Select distinct records in mysql

My table
ANONYMOUS
ONE TWO
1 2
2 1
1 2
3 1
Now i want to select distinct set of one and two.
My selected list should be
ANONYMOUS
ONE TWO
1 2
3 1
Your question isn't very clear, but I guess you mean this:
SELECT DISTINCT one, two
FROM yourtable AS T1
WHERE one <= two
OR NOT EXISTS
(
SELECT *
FROM yourtable AS T2
WHERE T1.one = T2.two
AND T1.two = T2.one
)
It finds rows with (one, two) where the reversed pair (two, one) does not exist. If both exist, it chooses the pair such that one < two. It also selects rows where the values are equal.
See it working online: sqlfiddle
If you would prefer to use a JOIN instead of NOT EXISTS you can do that:
SELECT DISTINCT T1.one, T1.two
FROM yourtable AS T1
LEFT JOIN yourtable AS T2
ON T1.one = T2.two
AND T1.two = T2.one
WHERE T1.one <= T1.two
OR T2.one IS NULL
See it working online: sqlfiddle
SELECT DISTINCT a.*
FROM `ANONYMOUS` a
LEFT JOIN `ANONYMOUS` b ON (a.one=b.two and a.two=b.one)
WHERE b.one is null or a.one<b.one
ORDER BY 1,2

How do you COUNT outside of the WHERE statement in MYSQL

I have a table - something like:
A|B
1|1
1|2
1|3
2|1
2|3
2|4
2|5
3|5
My query returns the distinct values in A if they coincide with a value in B of 1 or 2 - so
A
1
2
I am trying to also return the original count of the 1's and 2's in column A - to get something like
A|Count
1|3
2|4
Is there a simple way to get this count please?
However, the COUNT (A) returns the number of A's coinciding with the initial WHERE statement:
A|Count
1|2
2|1
Thanks!
My SQL might be a bit rusty, but I think you can do:
SELECT A, count(*) AS Count FROM MyTable WHERE B IN (1, 2) GROUP BY A;
Another way:
SELECT a, (SELECT count(*) FROM t t2 WHERE t2.a = t.a) a_count
FROM t
WHERE b IN (1,2)
GROUP BY a
SELECT t1.A, COUNT(DISTINCT t1.B)
FROM MyTable t1 JOIN MyTable t2 ON (t1.A = t2.A)
WHERE t2.A = t2.B
GROUP BY t1.A;
Similar to Bill Karwin's answer:
SELECT
A,
Counted.CountOfB
FROM
MyTable
INNER JOIN (
SELECT A, COUNT(B) AS CountOfB
FROM MyTable
GROUP BY A
) Counted ON Counted.A = MyTable.A
WHERE
{your filter for MyTable}