This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
MySQL, PHP: Select * from table where id is not in array
(3 answers)
Select all field where field value not in array
(3 answers)
Closed 3 years ago.
I am writing an SQL query. I have an array of unknown length and I want to select the data fromMySQL by using that array in the WHERE clause of the query. This is my query right now and it is working fine
$sql = "SELECT DISTINCT messagesutou.SenderID from messagesutou where (messagesutou.SenderID !='$items[1]' AND messagesutou.SenderID !='$items[0]' AND messagesutou.SenderID !='$items[2]') AND messagesutou.RecieverID='$uid'";
But in this I know the length of array ( 3) and I just used the array name with index for testing purpose. Now i want to know if array length is unknown then how would I write this query?
$list = implode(',', $items);
and
SELECT DISTINCT SenderID
FROM messagesutou
WHERE 0 = FIND_IN_SET(SenderID, '$list')
AND RecieverID='$uid'
or (taken from Jens's answer which was deleted by him)
SELECT DISTINCT SenderID
FROM messagesutou
WHERE SenderID NOT IN ($list)
AND RecieverID='$uid'
The difference - both variants are applicable when SenderID and $items values have a numeric type, only the former when they have string type, none when they have string type and contain commas or ticks.
But the latter may be adapted:
$list = '\''.implode('\',\'', $items).'\'';
and
SELECT DISTINCT SenderID
FROM messagesutou
WHERE SenderID NOT IN ($list)
AND RecieverID='$uid'
It now acccepts any datatype and allows commas (but not ticks - they must be quoted before imploding).
Related
This question already has an answer here:
Why SUM of column values return 1?
(1 answer)
Closed 9 days ago.
I have a problem, I need to convert from an array to a full number, but I don't understand how. i`m using redbeanphp. Help please
print_r(R::getRow('SELECT SUM(view) FROM posts WHERE author LIKE ? LIMIT 1', ['dffdfghdfgdf']));
Result:
Array ( [SUM(view)] => 27 )
if through echo, then it simply gives an error that it is an array.
Result:
Warning: Array to string conversion in
You should print the value of the array element, not the array itself.
Also, you may use an alias for a result column using the as operator.
$result = R::getRow('SELECT SUM(view) as s FROM posts WHERE author LIKE ? LIMIT 1', ['dffdfghdfgdf']);
print_r($result['s']);
echo would work either in this case.
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
I want to select the name contained in the column with the same name as my variable $column_id, which I pass to the function displayed below. It's passed correctly but without putting the '' around it the query results in an error. If I put them the result of the select is the value of $column_id itself, which is wrong. (the syntax equals to the one for prepared queries because that's the next step, if I can fix this issue)
$nirk2 = $this->conn->prepare("SELECT '" .$column_id. "' FROM t_values WHERE device_id='".$device_id."'");
$nirk2->execute();
$nirk2->bind_result($Value_Description);
$nirk2->fetch();
All I want to do is basically use my variable $column_id as name of the column to search the value in.
If you want to use a string as a column name you need to ensure that this column actually exists (is whitelisted) and then wrap it using backticks.
// whitelist column name
if(!in_array($column_id, ['my_col 1', 'my_col 2'])){
throw new \Exception('Invalid column name!');
}
// V backticks V
$nirk2 = $conn->prepare("SELECT `" .$column_id. "` FROM t_values WHERE device_id=?");
$nirk2->bind_param('s', $device_id);
$nirk2->execute();
$nirk2->bind_result($Value_Description);
$nirk2->fetch();
This question already has answers here:
Querying MySQL with IN clause using PHP [duplicate]
(4 answers)
php mysql IN clause not working with CSV variable. only first row is affected
(1 answer)
Laravel Eloquent "WHERE NOT IN"
(12 answers)
Closed 3 years ago.
Currently I have this variable
$arrays = implode(", ", $request->pmChck);
and If I try to return this variable. I will be getting this kind of output
2019-100,2018-100
As you can see the values are separated by commas
Now in my laravel query, I'm trying to get all the records of the employee except for these two company_id above.
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN('".$arrays."')");
The query is not working, it shows all the data and also the data with the company_id of 2019-100 and 2018-100
It should listing all the data except for these two company_id 2019-100 and 2018-100
Is there anything wrong with my format or syntax?
Assuming $request->pmChck is an array of companies you want to exclude, the query will be :
DB::table(..)->select(..)->whereNotIn('company_id', $request->pmChck)->get();
Your imploded array looks like
2019-100, 2018-100
and so is query:
SELECT * FROM view_employee_info WHERE company_id NOT IN('2019-100, 2018-100')
try imploding like this:
implode("', '", $arr)
And query will be ok:
SELECT * FROM view_employee_info WHERE company_id NOT IN('2019-100', '2018-100')
The problem in your query are the single quotes in the IN part.
If you change from
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN('".$arrays."')");
To
$pm_selected = DB::connection('mysql')->select("SELECT * FROM view_employee_info WHERE company_id NOT IN(".$arrays.")");
it should work as you expect it. It currently does not work, because your query would be like the following:
SELECT * FROM table WHERE company_id IN ('123, 234')
which would treat your input values as a single value, 123, 234
This question already has answers here:
MySQL query String contains
(8 answers)
Closed 7 years ago.
I tried using LIKE and CONTAINS, but neither worked
My column named Column in my table contains "asdl, test3, asdklj, dlksj" and I want to find "test3" in there using this string "test2, test3, test4"? How can I do this?
$string = "test2, test3, test4";
"SELECT * FROM table WHERE CONTAINS(Column, ".$string.")"
Another option is to use LIKE, but doesn't work either:
$string = "test2, test3, test4";
"SELECT * FROM table WHERE Column LIKE '%".$string."%'"
**
In summary I need to find a word in $string that matches with a word in Column.
**
Can be done using like. If your strings are comma seperated values then do it like this:
SELECT
* FROM table
WHERE
column LIKE "test3,%" OR
column LIKE "%,test3,%" OR
column LIKE "%,test3"
This question already has answers here:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given [duplicate]
(2 answers)
Closed 8 years ago.
I have a mysql query which doesn't work. It gives me the following error:
mysqli_query() expects parameter 1 to be mysqli, null given
my sql query is:
$select = mysqli_query($sql, "SELECT title FROM category WHERE id LIKE (SELECT categorie_id FROM categories_sub WHERE file LIKE '".$site."')")
The query will not work, what your query is doing
WHERE id LIKE (SELECT categorie_id FROM categories_sub WHERE file = 'some val')
This is similar as
WHERE id = {multiple categorie_id}
when the subquery has more than one categorie_id and this will return error.
So replace
category WHERE id LIKE
to
category WHERE id IN ( ...
This error tends to mean that you didn't connect correctly to the database. Double check your connection. Also, file LIKE 'x' is equivalent to file='x'. To search for a string containing 'x', use file LIKE '%x%'.
Edit: The other answers are right that you need to use IN instead of LIKE for the first one (i.e. a member of the set defined on another query)