Change mysql query to laravel 5.4 - mysql

I have a mysql query
SELECT * FROM Appraisal_skills WHERE INSTR(`assigned_to`, 6);
Which working well.I want to change this to laravel
I tried
$getskillset=Db::table('Appraisal_skills')->where(`assigned_to`, 6)->instr()->get();
But got an error like BadMethodCallException in Builder.php line 2508:
Call to undefined method Illuminate\Database\Query\Builder::instr()
Please correct me.Any help would be appreciated.

Laravels database query builder doesn't have instr method. But you can use whereRaw method.
So your code will be like this
$getskillset=Db::table('Appraisal_skills')->whereRaw('INSTR(`assigned_to`, ?)', [6])->get();
You can read more about raw expressions in Laravel docs
and here is api documentation

Related

SQLAlchemy - Unable to reflect SQL view

I'm getting the following error message when trying to reflect any of my SQL views:
sqlalchemy/dialects/mysql/reflection.py", line 306, in _describe_to_create
buffer.append(" ".join(line))
TypeError: sequence item 2: expected str instance, bytes found
I have tried using both the autoload_with and autoload=True options in my select query constructor to no avail.
I have the appropriate permissions on my view. My query is pretty simple:
company_country = Table('company_country', metadata, autoload_with=engine)
query = select(company_country.c.country)
return query
I've tried the inspect utility and it does not list my SQL view, nor does the reflecting all tables described below the views section on this page: https://docs.sqlalchemy.org/en/14/core/reflection.html#reflecting-views
I'm using version SQLAlchemy->1.4.32, Python 3.x and mySQL 8.0.28 on Mac if that's any help
I should add that I can query my SQL views using the text() constructor but it would be far more preferable to use select() if possible.
Any tips appreciated
I was using the mysql-connector client for interop with other code, but after switching to the mysqlclient, I was able to reflect the views.
https://docs.sqlalchemy.org/en/14/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb

MySQL CONCAT() condition NETBEANS error

So im trying to make a search function for my application
and i have this line of code where in im having a problem with:
String searchQuery = "SELECT id,**CONCAT(first,' ',last)**
FROM `person`
WHERE CONCAT(`id`,`first`,`last`) LIKE '%"+ValToSearch+"%'";
when i run my program and test the function the console returns this error
Column 'first' not found(Line 1).
if i remove the concat function and just use 'first' it does work. However i want it so that it shows both first and last name of the person
Thanks in advance.
Found the solution to my own problem.
Seems like the 2nd concat at the where statement uses the alias for the 1st concat.

Symfony 2 - Doctrine ORM Update query not working

I am trying to update a mysql table with following query using Doctrine. But the table is not get updated. Also below code didnt throw any error. I am totally confused. If i run the query, taken from getDQL() method, in mysql directly it showing 0 rows updated becuase of inproper qoutes. it is working after placed proper qoutes for values in the query. Need help to solve this puzzle.
Since i am new to doctrine, i will use the examples give in querybuilder class file.
$support = $this->createQueryBuilder('p')
->update('gcns', 'g')
->set("g.isActive", "0")
->andWhere("g.issn='".$issn."'");
Do you ever execute the query or are you just building it? You should have something along these lines to execute it:
$support->getQuery()->getSingleScalarResult();
If i run the query, taken from getDQL() method, in mysql directly it showing 0 rows updated becuase of inproper qoutes.
getDQL() returns DQL not SQL, so it will have improper quotesif you try to run it directly inside MySQL, but that's expected.
You shouldn't concatenate $issn into your query. You should use parameters instead:
$qb = $this->createQueryBuilder()
$support = $qb->update('gcns', 'g')
->set('g.isActive', '0')
->andWhere( $qb->expr()->eq('g.issn', ':issn') )
->setParameter( 'issn', $issn )
->getQuery()->getSingleScalarResult()
;

In Kohana query builder is it possible to compare column to another column, rather then a string or other value?

I am trying to build a query in Kohana and struggling to convert the query given to me by a DB admin into kohana query builder syntax. The query is failing when trying to compare the following:
AND du.user_key = dd.user_key
Using the syntax
->and_where('du.user_key', '=', 'dd.user_key')
I know this is because the builder is expecting the params, column, op, value. SO my question is there away to get the builder to recognise the value as a DB column, i have been looking through the docs but but to no avail... Any help would be appreciated.
CHeers
You can use the DB::expression
http://kohanaframework.org/3.0/guide/database/query/builder#database-expressions
So in the case of this example:
->where('du.user_key', '=' ,DB::expr('dd.user_key'))

how to use string left function in hql

I have a sql query like this
select column from table where path = left('INPUTSTRING', length(path));
and trying to accomplish it in hql like this,
return session.createQuery("from Table where Path = left(:input, length(Path))").
query.setParameter("input", inputPath).
.list();
and getting an error like this
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: left near line 1
how to get this done? What is the corresponding string function in hql? Is there a solution for this using criteria query apis?
Yes, left() is not supported by the MySQLDialect. See the list of HQL supported functions on API docs.
Now you have left with 2 options.
Use session.createSQLQuery() method.
Create Your own Dialect class by extending the MySQLDialect and register the function there. This is told at hibernate forum here explained well in a blog post here.
I'm not sure if HQL does this for you , but you can use IQuery/session.CreateSQLQuery() to use a raw SQL query to populate a mapped entity. I've never used it for substrings, but have used it for aggregate functions. Check chapter 13 of the NHibernate docs and see if that does it for you. You can check the query substitution available in Nhibernate - here