Mysql query where value is more than a specific entity value - mysql

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)

Related

MySQL string used in ORDER BY FIELD

I have an array of values stored in a single cell as a comma-separated string, thus:
"0,9,10,2,7,3,4,5,6,8,1,11,12"
I also have a table (table1) with a column (order) with these numbers in a different sequential order:
id|order
0 |5
1 |3
2 |4
3 |1
4 |2
etc
What I want to do is SELECT the "order" string from one table (table2) and use those values in an ORDER BY FIELD command when SELECTing from table1.
This should return row in this order: 3,4,1,2,0.
What I have so far is:
SELECT id
FROM table1
WHERE table1_id = 3746
ORDER BY FIELD(orderKey, (
SELECT `order` FROM table2
WHERE table2_id = 3746)
);
The problem seems to be that the SELECTorderFROM table2 is returned as a string which ORDER BY FIELD doesn't want to accept as input. It does SELECT the rows, but they are not in the specified order.
I have tried type-casting, but I may not have done this correctly. I also have sought an equivalent to a php explode function, to no avail.
MySQL version is 5.6.41
Any assistance is appreciated.
You can use FIND_IN_SET() in the ORDER BY clause:
select *
from table1
order by find_in_set(`order`, (select `order` from table2 where table2_id = 3746))
This will work if all the values in the column order of table1 exist in the string returned by the subquery as you mention in the question:
I also have a table (table1) with a column (order) with these
numbers in a different sequential order
.
See the demo.
Results:
| id | order |
| --- | ----- |
| 4 | 2 |
| 1 | 3 |
| 2 | 4 |
| 0 | 5 |
| 3 | 1 |

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!

Difference between 2 selects in SQL

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);