SQL query: search value in array inside cell - mysql

I have tried a lot of syntax but nothing is working.
I have MySQL 8.0.31 with phpMyAdmin 5.1.1.
My table looks like this (the column I want to filter by):
The thing is, I'm saving in this field some order numbers, and I need to make a SELECT statement in which I have one order number (for example, 1632) and I need to determine if this number is stored inside the "array" of orders in any of the rows (in this example, the desired result of the query is the third row).
I have tried the IN statement without result.
If I use the LIKE statement obviously works, but in case of larger numbers (like 16320), if I'm searching for 1632, I will find also this value.
The separator of the numbers, if works with another one, is able to be changed.

I would do it as such
SELECT * FROM Orders
WHERE Ordernes LIKE '%,$searchValue,%' OR Ordernes
LIKE '$searchValue,%' OR Ordernes LIKE '%,$searchValue';
If you add the seperators on both sides you will only find the value that you are looking for and not numbers that look like it.

Related

SSRS - How to create a sum (total) of an expression column?

After plenty of research, it seemd I can't find a well-explained solution to my problem. I'll join some screenshots of my Tablix, group rows, and group columns to help you to understand.
First of all, here's my tablix. It's linked to a SharePoint List.
The expressions are all like this :
=Count(Fields!ID.Value, "NameOfTheGroup")
It counts the number of elements that are filtered by one of the group chosen in the expression, and it works perfectly fine at this point.
Now, here's my groups :
They all contains filters tomanage the count precisely to the need.
What I want to do : In the last row, I want to display the sum of each column that is in a group row. But theses rows aren't datasets fields, they are just only expressions, and I can't find a way to create a proper total of each column. I heard of Running Value but the few solutions I found about it didn't work.
Thanks for the future help.
EDIT: Actually, the problem seems to be much more complex: any rows I create under my group rows are invisible : if I create a new row under the "Classification", outside the "Classification" row group, the expression I insert in the "CDI", "CDD", etc... columns stay blank. I tried with various expression or just values like "Hello World".

Is it possible to use IFNULL() with a * in a SELECT statement with MySQL? If not, what is my alternative?

Here is what I have that currently works in a SELECT statement:
SELECT
movieitemdetails.barcode,
IFNULL(movieitemdetails_custom.title, movieitemdetails.title) AS title
FROM
...
The problem is that I need to have my SQL be more dynamic when it comes to column names and the amount of columns I have. I need to use * to dynamically pull back all rows. Here is some example pseudocode to show you what I need:
IFNULL(movieitemdetails_custom.*, movieitemdetails.*) AS *
I need it to bring back all of the columns with the same name on each table and do the IFNULL comparison. Obviously the code above is not correct.
Does anyone have any ideas on how to make what I have more dynamic involving *?
Note to certain people: Please don't give me flak about how I shouldn't use *. I know in general use it's not recommended, but in this specific project it is what we're going with.
No, the IFNULL() function accepts only two scalar expressions, not wildcard expressions like *.
To make this query more dynamic, your options are:
Build the query with application code, appending an IFNULL() expression for each such pair of columns. Then submit the query.
Or:
Fetch all the columns independently with SELECT * ... and then sort out which ones to use in application code.
You would have to do each column individually, like so:
SELECT
movieitemdetails.barcode,
IFNULL(movieitemdetails_custom.title, movieitemdetails.title) AS title
IFNULL(movieitemdetails_custom.col2, movieitemdetails.col2) AS col2
IFNULL(movieitemdetails_custom.col3, movieitemdetails.col3) AS col3
FROM
...
Not sure exactly what you mean by having it be more dynamic regarding the amount of columns though.. a SELECT statement cannot have a variable number of columns, so if you want that behavior you would need to use a stored procedure or build your SQL dynamically.

Mysql sum as last row

Is it possible to have the SUM of all numaric fields in the last of a set of rows?
As of now, I'm using a very simple query such as:
SELECT
*,
SUM((UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start))/3600)
FROM
times
in SQL you cant have a column that appears in only one row, likewise, you also cant have a row that doenst contain all the columns from the other rows.. So having a row that contains something unique is not possible. You can, however, add the calculated column to all rows in the dataset or do the calculation in the calling code after the data is returned.
I think what you are looking for is GROUP BY WITH ROLLUP you will find details on that in the MySQL manual.

SQL: Using LIKE, return row if search matches any value in that row

I'm trying to make a query where my LIKE can match any field in the row.
To put it another way, I don't want to explicitly set column names to match each LIKE condition.
So, like this:
SELECT * FROM mytable WHERE any_field_value LIKE 'str%'
NOT like this:
SELECT * FROM mytable WHERE col_a LIKE 'str%' OR col_b LIKE 'str%'
and so on.
Surely, not an uncommon request? Can someone show me the light?
Actually that's simply not true. It can be done, assuming you have set up full text indexing.
Once again, assuming we're dealing with sql server.
http://msdn.microsoft.com/en-us/library/ms142488.aspx
I should point out, since it isn't very clear in the linked document, that you can specify * rather than column names in order to search all of the full text indexed columns in the table.
EDIT:
Full text search is available in mysql as well. It's not clear to me whether or not * can be used with it though.
Can only be done using dynamic SQL (unless using full text indexing).
Before you do that, I suggest reading: The Curse and Blessings of Dynamic SQL (assuming SQL Server)

MySQL: Use aggregate result in further calculation

I'm currently working on writing report generators. For one report I need to do a breakdown by a given characteristic (supplier, logging user, language, etc which for each row includes the name of the characteristic I'm interested in, the number of items that match that characterastic, and the percentage of total items this figure represents. The first two aren't a problem, the third is.
For example, to get a breakdown by language I'd be using a query like this.
SELECT lang_id,
COUNT(IF(open=TRUE,1,NULL)) AS lang_total
FROM table
GROUP BY lang_id;
This gives me the number of items per language.
I can get the total number of items in the table and store it in a variable simply enough with a plain count.
SELECT #totalOpen:=COUNT(*) FROM table WHERE open = TRUE;
Now I want to add a third column, which is the figure in lang_total divided by the value in #totalOpen multiplied by 100 (in other words, the percentage of all items that fit the criteria). Something along the lines of the following:
This is the bit I'm having trouble with, as because as far as I can tell you can't use aggregate columns in calculations.
SELECT lang_id,
COUNT(IF(open=true,1,NULL)) AS lang_total
(lang_total/#totalOpen)*100 as lang_percent
FROM table
GROUP BY lang_id;
I'm sure that there must be a way of doing this in MySQL, but I've not been able to track it down. Can anyone help out with this?
I read this question now for the first time. I know that probably it's too late to be useful for you but I would have solved in this way.
select lang_id,
sum(if(open= true,1,0)) as lang_total,
coalesce(sum(if(open= true,1,null)) / #r,0) as percentage
from table,(select #r:=count(*) from table where open = TRUE) as t
group by lang_id;