SQL - Select column with certain value in it and other random values - mysql

Let me say i have a table called test with the following data
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
| 5 | 864296100119956 |
What i want to do is to be able to write a select statement that returns a 3 rows with two random values and one mandatory value from the t_number column
for example if the mandatory value is 864291100247345 the output should something like below
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 4 | 864296100098325 |
OR
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
I have tried the below query but it's not yielding the output i expect, in a sense that it does return a result but without the mandatory value
SELECT * FROM test WHERE t_number = 864291100247345 OR id LIMIT 3;
What is the best way to go about this?
Thank you.

You can use order by:
SELECT t.*
FROM test
ORDER BY (t_number = 864291100247345) DESC,
rand()
LIMIT 3;
This returns the mandatory number first and then random numbers after that.
MySQL treats boolean values (the result of the = expression) as numbers in a numeric context, with "1" for true and "0" for false. So the first expression in the order by sorts the result set with the "true" conditions first, followed by the others.

Related

Find the max value in the last digit

I am using MySQL 5.5. I am facing a problem on how to find the max value in the last digit.
For example below the table, I want to get the max value is detected the last digit. The result should be 100-1-15
Table name: abc
+----+------------+
| id | code |
+----+------------+
| 1 | 100-1-1 |
| 2 | 100-1-2 |
| 3 | 100-1-15 |
| 4 | 100-1-6 |
| 5 | 100-1-3 |
| 6 | 100-1-5 |
| 7 | 100-1-9 |
+----+------------+
I am using below the SQL query, but doesn't work:
SELECT id,max(code) FROM abc;
Hope someone can guide me how to solve it and can get the max code is 100-1-15. Thanks.
SELECT *
from abc
order by SUBSTRING_INDEX(code, '-', -1) + 0 desc
limit 1
Try
Select
id,
code
from abc
order by max(CAST(SUBSTR(code, 7, LENGTH(code)-6) AS SIGNED))
limit 1;

MySQL bitwise comparison

I have mysql with a user table with answers from a poll saved as a bitwise. How do I find the user with most or least common answers with the reference bitwise?
+------+---------+--+
| User | Answers | |
+------+---------+--+
| A | 1 | |
| B | 5 | |
| C | 10 | |
+------+---------+--+
Assuming by 'reference bitwise' you mean that you have another value that is a bitmask that you are trying to match against the Answers column, something like this should do it for you. In this case, I'm using '4' as the reference bitmask and myTable as the name of your table..
SELECT User, BIT_COUNT(Answers & 4) AS MatchedBits FROM myTable ORDER BY MatchedBits DESC
This returns:
+------+-------------+
| User | MatchedBits |
+------+-------------+
| B | 1 |
| A | 0 |
| C | 0 |
+------+-------------+
You can also add a LIMIT 1 clause to get just the top result, but of course that won't tell you if there is more than one top result with the same number of bits matched.

mySql Max function ot returning exact result

I don't know why i am not getting the exact result
SELECT MAX(MID(order_id,3,20)) As Id FROM `tbl_orders` WHERE `domain_id`=2
+------------+
| id |
+------------+
| 10121452 |
+------------+
Even i tried the same function without MID function
SELECT MAX(order_id) As Id FROM `tbl_orders` WHERE `domain_id`=2
+------------+
| id |
+------------+
| Hy10121452 |
+------------+
any my database have highest order
+--------+------------+
| id | order_id |
+--------+------------+
| 1 | Hy10121452 |
| 2 | Hy10121453 |
| 3 | Hy10121454 |
| 4 | Hy10121455 |
| 5 | Hy10121456 |
| 6 | Hy10121457 |
| 7 | Hy10121458 |
| 8 | Hy10121459 |
| 9 | Hy10121460 |
+--------+------------+
i have to increment in the highest number to generate new order No.
Is i am doing something wrong?
check your database -> table-> column the data does not contain the same values like this abc1 abc2 abc3 xxx1 if you differnt series then the result always wrong
Change MAX(MID(order_id,3,20)) to MAX(MID(order_id,3,))
Syntax:from W3School
SELECT MID(column_name,start[,length]) AS some_name FROM table_name;
where
column_name Required. The field to extract characters from
start Required. Specifies the starting position (starts at 1)
length Optional. The number of characters to return. If omitted, the MID() function returns the rest of the text
Since you want the highest id then you can use order by DESC and limit
SELECT order_id As Id FROM `tbl_orders` WHERE `domain_id`=2 ORDER BY order_id DESC LIMIT 1
Problem Solved i just have to rectify Order_id column it contains the duplicate entries. duplicate entries removed and problem solved like a charm.

Select a record n times which n = times of occurrences

I want to select a record n times in which n is the number of times a string has occurred in a field.
Example:
mytable:
+--------+------------------------------------+
| id | content |
+--------+------------------------------------+
| 1 | This string contains two strings. |
| 2 | This is a string. |
| 3 | This does not contain our keyword. |
+--------+------------------------------------+
Now I want the result of such a hypothetical query to be like the following result:
/* hypothetical: this won't yield the desired result obviously */
SELECT * FROM mytable WHERE content LIKE "%string%";
+--------+------------------------------------+
| id | content |
+--------+------------------------------------+
| 1 | This string contains two strings. |
| 1 | This string contains two strings. |
| 2 | This is a string. |
+--------+------------------------------------+
Is this even possible?
Thanks

SQL 'COUNT' not returning what I expect, and somehow limiting results to one row

Some background: an 'image' is part of one 'photoshoot', and may be a part of zero or many 'galleries'. My tables:
'shoots' table:
+----+--------------+
| id | name |
+----+--------------+
| 1 | Test shoot |
| 2 | Another test |
| 3 | Final test |
+----+--------------+
'images' table:
+----+-------------------+------------------+
| id | original_filename | storage_location |
+----+-------------------+------------------+
| 1 | test.jpg | store/test.jpg |
| 2 | test.jpg | store/test.jpg |
| 3 | test.jpg | store/test.jpg |
+----+-------------------+------------------+
'shoot_images' table:
+----------+----------+
| shoot_id | image_id |
+----------+----------+
| 1 | 1 |
| 1 | 2 |
| 3 | 3 |
+----------+----------+
'gallery_images' table:
+------------+----------+
| gallery_id | image_id |
+------------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 3 | 1 |
| 4 | 1 |
+------------+----------+
What I'd like to get back, so I can say 'For this photoshoot, there are X images in total, and these images are featured in Y galleries:
+----+--------------+-------------+---------------+
| id | name | image_count | gallery_count |
+----+--------------+-------------+---------------+
| 3 | Final test | 1 | 1 |
| 2 | Another test | 0 | 0 |
| 1 | Test shoot | 2 | 4 |
+----+--------------+-------------+---------------+
I'm currently trying the SQL below, which appears to work correctly but only ever returns one row. I can't work out why this is happening. Curiously, the below also returns a row even when 'shoots' is empty.
SELECT shoots.id,
shoots.name,
COUNT(DISTINCT shoot_images.image_id) AS image_count,
COUNT(DISTINCT gallery_images.gallery_id) AS gallery_count
FROM shoots
LEFT JOIN shoot_images ON shoots.id=shoot_images.shoot_id
LEFT JOIN gallery_images ON shoot_images.image_id=gallery_images.image_id
ORDER BY shoots.id DESC
Thanks for taking the time to look at this :)
You are missing the GROUP BY clause:
SELECT
shoots.id,
shoots.name,
COUNT(DISTINCT shoot_images.image_id) AS image_count,
COUNT(DISTINCT gallery_images.gallery_id) AS gallery_count
FROM shoots
LEFT JOIN shoot_images ON shoots.id=shoot_images.shoot_id
LEFT JOIN gallery_images ON shoot_images.image_id=gallery_images.image_id
GROUP BY 1, 2 -- Added this line
ORDER BY shoots.id DESC
Note: The SQL standard allows GROUP BY to be given either column names or column numbers, so GROUP BY 1, 2 is equivalent to GROUP BY shoots.id, shoots.name in this case. There are many who consider this "bad coding practice" and advocate always using the column names, but I find it makes the code a lot more readable and maintainable and I've been writing SQL since before many users on this site were born, and it's never cause me a problem using this syntax.
FYI, the reason you were getting one row before, and not getting and error, is that in mysql, unlike any other database I know, you are allowed to omit the group by clause when using aggregating functions. In such cases, instead of throwing a syntax exception, mysql returns the first row for each unique combination of non-aggregate columns.
Although at first this may seem abhorrent to SQL purists, it can be incredibly handy!
You should look into the MySQL function group by.