SQL Query WHERE clause is a parameter - mysql

Can you please help me solve the problem I am having with my query. I am very new in Database programming and I thoroughly search the web for the help but none solve my issue. I am fed up and wondering if this is even possible.
What I want to accomplish is to execute a SELECT query where the column to search is to be supplied on runtime. Like this one:
SELECT *
FROM myTable
WHERE #columnToSeach = #_ColumnName
Advice is really appreciated as my brain is practically bleeding on this one.

A very quick solution would be:
execute ('SELECT * FROM myTable WHERE ' + #columnToSeach + '=' + #_ColumnName)

Related

Is it possible to add two values in an OnConflict clause in GORM?

Looking to do something like
UPDATE `items` SET `book_id`=`book_id` + ValueInUpdateRecord
in the DoUpdates part of the OnConflict clause in GORM before calling Create(&records)
I can do it in raw SQL no problem, but I'd prefer figure out the difficult way :)
Hope this helps someone. Makes sense now that I've done it, but trying to find any info on this topic is a lost cause! This will do a bit-wise OR in mysql to enable a specific bit, but can be translated to any sort of expression.
bookNumber := 32
db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.Assignments(map[string]interface{}{
"book_id": gorm.Expr("`book_id` | " + fmt.Sprintf("%d", bookNumber))
}),
}).Create(&bookItems)

Why does a MySQL not recognize a table value?

Friends
For some reason, a MySQL query refuses to recognize a particular value in a table.
The table contains columns
"idLookUp", "LUgroup", "LUvalue"
212 adispo AdmICU_Interv
213 adispo AdmICU_noInterv
SELECT * FROM LookUp WHERE LUvalue = "AdmICU_Interv";
returns no records!
SELECT * FROM LookUp WHERE LUvalue = "AdmICU_noInterv";
returns the proper record (#212).
SELECT * FROM LookUp WHERE idLookUp = 212
returns the proper record
The string AdmICU_Interv doesn't figure anywhere else and is not a reserved phrase (from what I can tell). I'm sure I'm missing something stupid here, but I can't figure out what is causing this behavior.
I'd appreciate any hints. Thx!
jon
could you have some hidden char try using trim()
SELECT * FROM LookUp WHERE trim(LUvalue) = "AdmICU_Interv";
or like
SELECT * FROM LookUp WHERE LUvalue like "%AdmICU_Interv%";
and this:
SELECT * FROM LookUp WHERE LUvalue LIKE "%AdmICU_Interv%"
Thanks Folks, very helpful. Yes, there was something in there I could not see. Still don't know what. Length was 13 when it should have been 12. Overwriting with the correct string solved the problem.
Since this was a lookup table, I have to go back and update all records with the problematic entry, but MySQL does that easily:
UPDATE LookUp SET LUvalue = 'AdmICU_Interv' WHERE LUvalue LIKE "%AdmICU_Interv%";
I do appreciate the problem solving hints. Hope this helps somebody else.
Cheers
Jon

SQL getting two 'TYPE'

I would like to get two of the same value when I Query my database.
I do not have amazing SQL knowledge so I wouldn't know where to start doing this so i'm asking for help from this friendly community.
To help me explain what I am looking for I will show you what I mean.
"SELECT * FROM $databasename WHERE type='post' and type='friend_post' ORDER BY time desc"
(i put the area I am looking at in darker font)
This is what I am trying to do but it dosen't seem to work. Thanks in advance for any help I get, it will be much apreciated.
This is because you check if type is both value, use IN instead.
Which would looks like
SELECT *
FROM $databasename
WHERE type IN('post','friend_post')
ORDER BY time desc

SELECT * FROM games WHERE

I am having issues with my MySQL syntax. I would like to run a select query where either one of two options are true. However the following code does not work.
SELECT * FROM games WHERE genre="indie" OR title="indie"
I have been fooling around and look at other threads and have found out how to use OR to check the same column for multiple entries but not a way to check different columns for the same entries. When I do:
SELECT * FROM games WHERE genre="indie"
The query works fine. Any help would be greatly appreciated.
The only way I see this really would't work, is if you've mistyped the name of the column 'title' (if the second query you wrote works)
The assumptions about the case sensitivity are wrong, since the second query returns something, the first should return at least the same rows as the second one
In MySQL " " works just as ' ', so this assuption was wrong too.
If you post more information, it would be easier to help you
Maybe you ignoring the upper/lower case? Also use like
You can use this:
SELECT * FROM games WHERE (LOWER(genre) like 'indie') OR (LOWER(title) like 'indie')

Using REPLACE() in MySQL SELECT

I apologise in advance for asking what I'm sure will prove to be a very simple question.
I have a MySQL (5.5) database that includes, amongst other things, a field for telephone numbers. I'm trying to create a statement that will search that field, stripping out any spaces. So searching for '0208' will return '020 8', '02 08', '0 208', ' 0208', etc.
And this is in Delphi XE2, in case that makes a difference.
'SELECT * FROM sales_ledger WHERE REPLACE(telephone, " ", "") LIKE "%' + SearchEdit.Text + '%"'
...gives me an error...
Invalid filter in WHERE clause
...and...
'SELECT REPLACE(telephone, " ", "") FROM sales_ledger WHERE REPLACE(telephone, " ", "") LIKE "%' + SearchEdit.Text + '%"'
...gives me...
Invalid field name. General SQL error. Column not found.
...and I do actually need all fields returned anyway.
May I ask for some assistance in correcting the syntax please. If you need more information, don't hesitate to ask. Thanks very much for your time.
EDIT: One potentially critical piece of information I missed out. The table is actually a Sage database that I'm accessing through ODBC. As nothing I try is working that may well be the root problem. Apologies for not saying that earlier.
Your Query Seems Working Fine see the demo
First try this query to you DB Client
SELECT * FROM sales_ledger
WHERE REPLACE(telephone, " ", "") LIKE "%0208%"
EDIT:
if you query is still not working. fellow step by step process.
try to run a simple select query (SELECT * FROM sales_ledger)
if the first query run try adding simple where condition.
so step by step you can make your query similar to original one and find where is the actual error from.
Try this ::
SELECT
REPLACE(telephone,' ', '') as replacedColumn
FROM sales_ledger
WHERE REPLACE(telephone,' ', '') LIKE '%"+ SearchEdit.Text +"%'"
OR
Select temp1.*
from
(
SELECT
REPLACE(telephone,' ', '') as replacedColumn
FROM sales_ledger
) as temp1
WHERE temp1.replacedColumn LIKE '%"+SearchEdit.Text+"%'"