MYSQLi SELECT statement malformed or misordered - php

Using the following table "technotes":
And, using the following SQLi Query:
SELECT *
FROM
`technotes`
WHERE
`techgroup` LIKE '%dispatch%' OR
`techgroup` LIKE '%30243542%' AND
`expires` >= '2014-12-18' AND
`viewed` NOT LIKE '%30243542%'
What was expected was that alertid's: 23324325 & 23546576 (1st and last one) would be returned. But instead i am getting record 1,3,4,5 being returned. I am missing something in the order of operation or some other component in the SQLi select statement is malformed but not sure what. Can someone advise what I have left out or need to change so this works properly?

Try this:
SELECT * FROM
`technotes`
WHERE
(`techgroup` LIKE '%dispatch%' OR `techgroup` LIKE '%30243542%')
AND `expires` >= '2014-12-18'
AND (`viewed` NOT LIKE '%30243542%' OR ISNULL(`viewed`) )

Related

Codeigniter select_sum generate wrong query

I worked on a Codeigniter 2 project, there I have an active record like:
$query = $this->db->select_sum('Cardit * GPA', 'sum')
->get('marks_info');
It generates query like:
SELECT SUM(`Cardit` * `GPA`) AS `sum` FROM `marks_info`
Recently I migrated my project to Codeigniter 3, but the same active record generated little different query like:
SELECT SUM(`Cardit *` `GPA`) AS `sum` FROM `marks_info`
that is wrong, it includes * with Cardit ('Cardit *') in SUM section.
Difference:
Can anybody tell me, how can I solve this issue in CodeIgniter 3 ?
Try this, It will give you your expected output
$query = $this->db->select('sum(Cardit * GPA) sum')->get('marks_info');
To avoid backticks within the query. You can assign false to protect_identifiers like :
$this->db->_protect_identifiers=false;
and run your query accordingly.

SQL WHERE CONCAT LIKE NOT

I try to make SQL to search some string in database.
In this spesification, The SQL must be dont display one string in database.
my sql like this :
$query = "SELECT * FROM `chatuser` WHERE CONCAT( `fullname`,`image`) LIKE '%".$search_string."%' NOT (`$string is not be displayed`) " ;
is that possible ?
Thanks for help
The correct syntax of LIKE and NOT LIKE as two conditions would be:
SELECT * FROM chatuser
WHERE CONCAT(CustomerName,ContactName) LIKE '%t%'
AND CONCAT(CustomerName,ContactName) NOT LIKE '%m%';
You miss AND Between conditions. Also you have to repeat CONCAT(CustomerName,ContactName).
In the example above we are looking for all CustomerName+ContactName with a t in any place but if it doesn't have an m in any place.
From the docs found at https://www.w3resource.com/mysql/comparision-functions-and-operators/not-like.php
Example: MySQL NOT LIKE operator with (%) percent
The following MySQL statement excludes those rows from the table author, having the 1st character of aut_name ‘W’.
Code:
SELECT aut_name, country
FROM author
WHERE aut_name NOT LIKE 'W%';
And so it seems would work in your situation.

Error trying to select field while not selecting first character

I'm getting the error: Incorrect syntax near the keyword 'AS'.
I'm trying to select a column from my MSSQL database while NOT SELECTING the first character of that field.
Below is the code (Within PHP):
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) FROM Table WHERE [Column] ='".$search."' AS Column2";
First of all. Please be careful. Your code looks vulnerable to an SQL injection. Better use paramterized queries.
The second "AS" at the end is not needed.
Try it that way:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS Revlv2 FROM table_name WHERE [Objkt] ='".$search."'
Or better yet:
SELECT RIGHT(Revlv, LEN(Revlv) - 1) AS column_name FROM table_name WHERE [Objkt] = ?
Read more about parameterized queries here
Also take care when the Revlv column only contains an empty string. The query will fail in that case.
try this:
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
As all answers simply fix the syntax instead of fixing the logic:
There's no need to use RIGHT + LEN to extract everything but the first character, simply use
substring(Revlv from 2) AS Revlv2 -- Standard SQL to extract everything after the first character
As SQL Server has a slighty different syntax:
substring(Revlv, 2, 8000) AS Revlv2 -- T-SQL to extract everything after the first character
You need to put AS after the SELECT part of your query never put it at the end of your query. Try to do it like this
$sql = "SELECT RIGHT(Column, LEN(Column) - 1) AS Column2 FROM Table WHERE [Column] ='".$search."' ";
SOURCE
try this.
$sql="SELECT RIGHT(Revlv, LEN(Revlv)-1) AS Revlv2
FROM tablename where['objkt']='$search'";

Using character # in SQL query request with PHP

I am using PHP for sending query request to GoogleBigQuery sql database.
The request would be similar like this:
SELECT namedcoord, COUNT(*)
FROM (
select REGEXP_REPLACE(REGEXP_EXTRACT(SPLIT(V2Locations,';'),r'^[2-5]#(.*?#.*?#.*?#.*?#.*?#.*?)#'), '^(.*?)#.*?#.*?#.*?#(.*?)#(.*?)', '\1;\2;\3') as namedcoord
from [gdelt-bq:gdeltv2.gkg]
where DATE>20150302000000 and DATE < 20150304000000 and V2Persons like '%Tsipras%'
)
where namedcoord is not null
group by namedcoord
ORDER BY 2 DESC
LIMIT 100
However, PHP can't handle the character # because it's treated as comment syntax.
Does anyone can help solve this problem?

How to use LIKE operator against multiple values

I have this query :
select * from users where mailaddress
NOT like '%banned_domain1.com%'
AND mailaddress NOT like '%banned_domain2.com%'
AND mailaddress NOT like '%banned_domain3.com%' ;
I want to make it more simple , I executed this query from command line :
select * from users where mailaddress
NOT like ('%banned_domain1.com%','%banned_domain2.com%','%banned_domain3.com%') ;
I got MySQL error :
ERROR 1241 (21000): Operand should contain 1 column(s)
You can use NOT REGEXP
SELECT * FROM users WHERE mailaddress NOT REGEXP 'banned_domain1.com|banned_domain2.com|banned_domain3.com';
See live demo
Instead of "Like" use "In" and format the email address like this:
select * from users where SUBSTR(mailaddress, INSTR(mailaddress, '#') + 1)
NOT IN ('banned_domain1.com','banned_domain2.com','banned_domain3.com');
The SUBSTR will remove the # and anything preceding it, leaving only the domain name then you can do a perfect comparison without wildcards using IN.
Cheers!
you have to mention the column every time
select * from tasks where title NOT LIKE '%eating lunch%' AND title NOT LIKE '%eating breakfast%' AND title NOT LIKE '%a new task%'
however as Bruno Domingues said use NOT IN that will be more easy
You cannot simplify your query. You need one LIKE per condition.

Categories