How to use if condition in SOQL? - mysql

How to reproduce the following statement of MySQL in SOQL:
SELECT IF(1 = 2,'true','false');
I am trying to do the following:
select if(field1=null,false,true) as res from table
But it is showing me unknown parsing error.

That is not possible in SOQL.
Not sure of the requirement here but you may use a formula field instead.

Related

how to include hard-coded value to output from mysql query?

I've created a MySQL sproc which returns 3 separate result sets. I'm implementing the npm mysql package downstream to exec the sproc and get a result structured in json with the 3 result sets. I need the ability to filter the json result sets that are returned based on some type of indicator in each result set. For example, if I wanted to get the result set from the json response which deals specifically with Suppliers then I could use some type of js filter similar to this:
var supplierResultSet = mySqlJsonResults.filter(x => x.ResultType === 'SupplierResults');
I think SQL Server provides the ability to include a hard-coded column value in a SQL result set like this:
select
'SupplierResults',
*
from
supplier
However, this approach appears to be invalid in MySQL b/c MySQL Workbench is telling me that the sproc syntax is invalid and won't let me save the changes. Do you know if something like what I'm trying to achieve is possible in MySQL and if not then can you recommend alternative approaches that would help me achieve my ultimate goal of including some type of fixed indicator in each result set to provide a handle for downstream filtering of the json response?
If I followed you correctly, you just need to prefix * with the table name or alias:
select 'SupplierResults' hardcoded, s.* from supplier s
As far as I know, this is the SQL Standard. select * is valid only when no other expression is added in the selec clause; SQL Server is lax about this, but most other databases follow the standard.
It is also a good idea to assign a name to the column that contains the hardcoded value (I named it hardcoded in the above query).
In MySQL you can simply put the * first:
SELECT *, 'SupplierResults'
FROM supplier
Demo on dbfiddle
To be more specific, in your case, in your query you would need to do this
select
'SupplierResults',
supplier.* -- <-- this
from
supplier
Try this
create table a (f1 int);
insert into a values (1);
select 'xxx', f1, a.* from a;
Basically, if there are other fields in select, prefix '*' with table name or alias

Select * from Select

Its very weird situation I know, nut I have got myself into it somehow. I have to connect to some other system service by passing some parameters in url.
In their service they are creating some query using parameter I pass.
For my case I have to pass 'Select' as a parameter name which is actually some class name on their side. So they end up in creating query as Select * from select
and some condition.
On execution I am getting error response as:
'There was a syntax error in a SQL query or filter expression at line
1, position 186. Saw \"Select\" but expected
'..SQL: \"SELECT col1, col2 FROM Select AS D where
some condition.
Can somebody help me on this.
Since Select is reserved word, you have to escape it by enclosing in backticks characters in order for MySQL to process your query:
select * from `select`
Its recommended not to use MySQL reserved keywords.. but if its necessary there is a solution..
Use this, it will work for you :
select * from yourdatabasename.select

What is the correct syntax for a Regex find-and-replace using REGEXP_REPLACE in MariaDB?

I need to run a regex find-and-replace against a column named message in a MySQL table named post.
My database is running MariaDB 10.
According to the docs, MariaDB 10 has a new REGEXP_REPLACE function designed to do exactly this, but I can't seem to figure out the actual syntax.
It will affect 280,000 rows, so ideally there's also a way to limit it to only changing one specific row at a time while I'm testing it, or simply doing a SELECT rather than an UPDATE until I'm sure it does what I want.
The regex I want to run:
\[quote\sauthor=(.+)\slink=[^\]]+]
The replacement string:
[quote="$1"]
The following was what I tried, but it just throws a SQL error:
UPDATE post SET message = REGEXP_REPLACE(message, '\[quote\sauthor=(.+)\slink=[^\]]+]', '[quote="$1"]') WHERE post_id = 12
In this case, the original message was:
[quote author=Jon_doe link=board=2;threadid=125;start=40#msg1206 date=1065088] and the end result should be [quote="Jon_doe"]
What is the proper syntax to make this REGEXP_REPLACE work?
You have to do a lot of escaping here:
REGEXP_REPLACE(message, "\\[quote\\sauthor=(.+)\\slink=[^\\]]+]", "\\[quote=\"\\1\"\\]")
Please note that you have to reference the Group by \\1

Unable to get mysql to recognise query using in statement

I've been attempting to adjust a mysql SELECT statement based on input from checkboxes. The php code collects the ticked checkboxes into an array, implodes them into a comma-separated list and then runs the query using an in statement (as was detailed here).
The query generated comes out as SELECT * FROM events WHERE Discipline IN (SJ,OTHER) which is a correctly formatted query as far as I can tell.
This shows up as an invalid query when run from the php code. When I run the query using phpmyadmin, I receive this message:
#1054 - Unknown column 'SJ' in 'where clause'
I was wondering if anyone could tell my why that query is generating the error?
Try putting quotes around the values in the IN clause:
SELECT * FROM events WHERE Discipline IN ('SJ','OTHER')
If SJ and OTHER are string literal use:
SELECT * FROM events WHERE Discipline IN ('SJ','OTHER')

Unknown column in 'field list' error on MySQL Update query

I keep getting MySQL error #1054, when trying to perform this update query:
UPDATE MASTER_USER_PROFILE, TRAN_USER_BRANCH
SET MASTER_USER_PROFILE.fellow=`y`
WHERE MASTER_USER_PROFILE.USER_ID = TRAN_USER_BRANCH.USER_ID
AND TRAN_USER_BRANCH.BRANCH_ID = 17
It's probably some syntax error, but I've tried using an inner join instead and other alterations, but I keep getting the same message:
Unknown column 'y' in 'field list'
Try using different quotes for "y" as the identifier quote character is the backtick (`). Otherwise MySQL "thinks" that you point to a column named "y".
See also MySQL 8 Documentation
Please use double-/single quotes for values, strings, etc.
Use backticks for column-names only.
Enclose any string to be passed to the MySQL server inside single quotes, e.g.:
$name = "my name"
$query = " INSERT INTO mytable VALUES ( 1 , '$name') "
Note that although the query is enclosed between double quotes, you must enclose any string in single quotes.
You might check your choice of quotes (use double-/ single quotes for values, strings, etc and backticks for column-names).
Since you only want to update the table master_user_profile I'd recommend a nested query:
UPDATE
master_user_profile
SET
master_user_profile.fellow = 'y'
WHERE
master_user_profile.user_id IN (
SELECT tran_user_branch.user_id
FROM tran_user_branch WHERE tran_user_branch.branch_id = 17);
Just sharing my experience on this. I was having this same issue. The insert or update statement is correct. And I also checked the encoding. The column does exist.
Then! I found out that I was referencing the column in my Trigger.
You should also check your trigger see if any script is referencing the column you are having the problem with.
In my case, it was caused by an unseen trailing space at the end of the column name. Just check if you really use "y" or "y " instead.
While working on a .Net app build with EF code first, I got this error message when trying to apply my migration where I had a Sql("UPDATE tableName SET columnName = value"); statement.
Turns out I misspelled the columnName.
If it is hibernate and JPA. check your referred table name and columns might be a mismatch
Just sharing my experience on this. I was having this same issue. My query was like:
select table1.column2 from table1
However, table1 did not have column2 column.
In my case, the Hibernate was looking for columns in a snake case, like create_date, while the columns in the DB were in the camel case, e.g., createDate.
Adding
spring:
jpa:
hibernate:
naming: # must tell spring/jpa/hibernate to use the column names as specified, not snake case
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
to the application.ymlhelped fix the problem.
In my case, I used a custom table alias for the FROM table, but I used the default table alias (MyTable) in the field list instead of the custom table alias (t1). For example, I needed to change this...
mysql> SELECT MyTable.`id` FROM `MyTable` t1;
...to this...
mysql> SELECT t1.`id` FROM `MyTable` t1;
In my case I had misspelled the column name in the table's trigger. Took me a while to connect the error message with the cause of it.
I too got the same error, problem in my case is I included the column name in GROUP BY clause and it caused this error. So removed the column from GROUP BY clause and it worked!!!
I got this error when using GroupBy via LINQ on a MySQL database. The problem was that the anonymous object property that was being used by GroupBy did not match the database column name. Fixed by renaming anonymous property name to match the column name.
.Select(f => new
{
ThisPropertyNameNeedsToMatchYourColumnName = f.SomeName
})
.GroupBy(t => t.ThisPropertyNameNeedsToMatchYourColumnName);
A query like this will also cause the error:
SELECT table1.id FROM table2
Where the table is specified in column select and not included in the from clause.