Difference between 2 selects in SQL - mysql

I have one data table:
--------------------
ID | user | Value
--------------------
1 | 1 | 1
--------------------
2 | 1 | 2
--------------------
3 | 2 | 3
--------------------
4 | 2 | 2
--------------------
5 | 3 | 4
--------------------
6 | 3 | 2
--------------------
I would like to SELECT all rows where value is different comparing to user 1 so the result would be rows with IDs 3 (value is 3) and 5 (value is 2)
I would do something like this (will call it A)
SELECT * FROM table WHERE user = 1
and get all the rows from user 1. Than I would select (will call it B)
SELECT * FROM table WHERE user != 1
and get all other rows. And than I would compare them WHERE A.value != B.value.
I'm stuck on how to merge everything together...
Please help!

Try this:
SELECT *
FROM table
WHERE value NOT IN ( SELECT value FROM table WHERE user = 1)

The relational operator is indeed 'difference', Oracle has the keyword MINUS, Standard SQL has the keyword EXCEPT e.g.
SELECT value
FROM table
EXCEPT
SELECT value
FROM table
WHERE user = 1;
Sadly, MySQL doesn't have any such an operator, you have to use other SQL constructs e.g. NOT IN <table expression>:
SELECT value
FROM table
WHERE value NOT IN ( SELECT value
FROM table
WHERE user = 1 );

select * from table where value not in (select value from table where user = 1);

Related

How to run alternative where condition if row is zero

Suppose i have a table
table
+-------------+
| id | name |
+-------------+
| 1 | xabx |
| 2 | abxd |
| 3 | axcd |
| 4 | azyx |
| 5 | atyl |
| 6 | aksd |
| 7 | baabc|
| 8 | aabcd|
+-------------+
first i have to get data if matches first some char like :
if name = aab
then have to run select * from table where name like 'aab%'
then it returns
+-------------+
| 8 | aabcd |
+-------------+
which execatlly i want
but if i have only abc
then the above query return 0 row
then i have to search from middle like :
select * from table where name like '%abc%'
then it returns which is the alternative
+-------------+
| 7 | baabc|
| 8 | aabcd|
+-------------+
i have no much knowledge about mysql is there any query which can do like if first where condition don't have row then run alternative where condition
i have tried this but didn't work as i want.
select * from table where name like 'abc%' or name like '%abc%'
fiddle
thanks in advance
This is somewhat your desired result:
select *from t
where (case
when name like 'abc' then 1
when name like 'bc%' then 1
when name like '%bc' then 1
when name like '%bc%' then 1
else null
end)
order by name
limit 1;
I just put all the combinations as conditions.
You can interchange their sequence or remove unnecessary condition.
limit 1 makes only 1 row visible for whichever condition satisfies.
Here is the answer from your fiddle. Check it out
Hope it helps!
This is a possible solution:
Left joining the table on itself, where the table is initially filtered by the more inclusive %bx% and then the join is filtered by the more restrictive bx%.
This allows you to use the joined name if it exists, but revert to the original if not:
SELECT t1.id, IF(t2.name IS NULL, t1.name, t2.name) name
FROM test t1
LEFT JOIN test t2 ON t2.id = t1.id AND t1.name like 'bx%'
WHERE t1.name LIKE '%bx%'
This may/may not be ideal depending on the size or your dataset.
COUNT checking may work
select *
from table
where name like 'aab%' or
((select count(*) from table where name like 'aab%') = 0 and name like '%abc%')
I guess that it would be a good idea to compute the count value into a variable first, however, the optimizer may recognize independent subquery anyway and run it once.

Is it possible to run CASE WHEN with pre queried data

I was wondering if it is possible to get the count of a query, plus check if a entity within the query exists at the same time.
So I was looking to do something like this
SELECT
COUNT(a.*)
CASE WHEN ? IN (a.column) THEN 1 ELSE 0 END AS exist
FROM a
WHERE ...
I know I could do a sub-query in the CASE, but is it possible to do it with just the data from the initial query?
EDIT
ie
+------------+
| id column |
+------------+
| 1 5 |
| 2 6 |
| 3 7 |
| 4 8 |
SELECT
COUNT(a.*)
CASE WHEN 7 IN (a.column) THEN 1 ELSE 0 END AS exist
FROM a
WHERE id > 1
Would return
+--------------------+
| COUNT(*) exist |
+--------------------+
| 3 1 |
Because there are 3 entries with an id > 1 and within the entries and there is an entry with column = 7
If you want to check if a value exists in the column, you can do:
SELECT COUNT(*),
MAX(? = a.column) as value_exists
FROM a
WHERE ...
In a numeric context, MySQL treats booleans as integers, with 1 for true and 0 for false. Hence, this returns true if the value is in any row in the column. You can use MIN() if you want to check if the same value is in all the rows.

Retrieve rows in which a column doesn't contain a value

I have a MySQL table like below:
| ID | userIDs
---------------
| 1 | 4,3,5
| 2 | 2,3
| 3 | 1,2,3
I want to retrieve all the rows in which userIDs doesn't contain 1.
I tried
SELECT * FROM tablename WHERE 1 NOT IN (userIDs)
But it's not working.
Use FIND_IN_SET
SELECT * FROM tablename
WHERE find_in_set(1, userIDs) = 0
But actually you should rather change your table design. Never store multiple values in a single column!

Mysql query where value is more than a specific entity value

TableA:
------
id | property|
--------------
1 | 0 |
2 | 5 |
3 | 6 |
Is it possible to query in mysql in single command to figure out which has a property value more than that where id=2?
Use a subquery to get the property value for id=2, and compare to that. The subquery must return just one row, which is the case here.
SELECT *
FROM TableA
WHERE property > (SELECT property FROM TableA WHERE id = 2)

MySql SET intersection

I have next database
| ID | numbers |
|--------|------------|
| 1 | 1,2,3 |
| 2 | 4,5,6 |
| 3 | 2,3,4 |
To retrieve all rows where numbers holds a given number is easy:
SELECT * FROM mydb WHERE FIND_IN_SET(3, numbers)
This returns row 1 and 3
But now I want to pass an array/set of numbers and want to retrieve all entries where at least one number occurs in numbers, i.e. where the intersect of both sets is not empty
Something like:
SELECT * FROM mydb WHERE SET_INTERSECT('2,4', numbers)!=NULL
This should return all rows because every row holds 2 and/or 4.
The above doesn't work. Is this possible in mysql?
Thanks!
Why not use OR ?
SELECT * FROM mydb WHERE FIND_IN_SET(2, numbers) or FIND_IN_SET(4, numbers)
If you change the table structure to:
ID | NUMBERS
1 | 1
1 | 2
1 | 3
2 | 4
2 | 5
...
SELECT DISTINCT id FROM table WHERE numbers = 2 OR numbers = 4
/*or...*/
/*WHERE numbers IN (2, 4)*/