Is it possible to do this in MySQL?
select * from keyword
where keyword NOT REGEX concat('\b', concat_ws('\b|\b', (select distinct(keyword) from negative_keyword)), '\b')
limit 3;
It currently gives this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'REGEX concat_ws('|', (select distinct(keyword) from negative_keyword)) limit 3' at line 1
The concat_ws by itself gives this error:
ERROR 1242 (21000): Subquery returns more than 1 row
I have to match whole words, not just substrings or exact matches. For example, negative keyword 'cat' should match 'black cat', but not 'catatonic'.
SELECT *
FROM x
WHERE NOT EXISTS
( SELECT *
FROM negative_keywords nk
WHERE nk.keyword = x.keyword
);
In the mean time, I had to filter them in code (PHP).
# filter negative keywords
$negatives = NegativeKeyword::find()->select('keyword')->distinct('keyword')->column();
$negatives = array_map(function($v) { return '/\b'.preg_quote($v).'\b/';}, $negatives); # escape regular expression, search whole words only
$kw = array_filter($kw, function($v) use ($negatives) {
foreach ($negatives as $n) {
if (preg_match($n, $v['keyword'])) { // strpos($v['keyword'], $n) !== false)
// echo "filtering $v[keyword]\n";
return false; // match found, filter word, break early
}
}
return true; // good to keep
});
Related
I have two tables: [Invoices], which has a field on Vendor ID "i_v_id," and [Inventory_adjustment], which has columns Debit, Credit, and flag value which is set to 0 by default.
In this situation, I need to update every row in the inventory_adjustment database that fits the WHERE requirements.
I tried several methods to update the rows, but they all resulted in error warnings.
Error Number: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '.inad_invoice_id SET inad_adjusted_flag = 1
WHERE IA.inad_date >= '2022-12' at line 1 UPDATE
inventory_adjustment IA JOIN invoices I ON I.i_id =
IA.inad_invoice_id SET inad_adjusted_flag = 1 WHERE
IA.inad_date >= '2022-12-25' AND IA.inad_date <= '2023-01-03'
AND I.i_v_id = '3'
Model
function Adjustment()
{
$data=array(
'IA.inad_adjusted_flag' => "1"
);
$datefrom = $this->input->post('datefrom');
$dateto = $this->input->post('dateto');
$this->db->where('IA.inad_date >=', $datefrom);
$this->db->where('IA.inad_date <=', $dateto);
$this->db->where('I.i_v_id', $this->input->post('vendor_id'));
$this->db->update('inventory_adjustment IA JOIN invoices I ON I.i_id = IA.inad_invoice_id',$data);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
getting a SQL syntax error all of a sudden:
Suddenly getting a SQL syntax error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1%'' LIMIT 18446744073709551615' at line 1'
// begin Recordset
$suggestParam__colours = '-1';
if (isset($_GET['suggest1_choice'])) {
$suggestParam__colours = $_GET['suggest1_choice'];
}
$query_colours = sprintf("SELECT colour_name FROM colours WHERE colour_name LIKE '%s'", GetSQLValueString($suggestParam__colours . "%", "text"));
$colours = $autocomplete->SelectLimit($query_colours) or die($autocomplete->ErrorMsg());
$totalRows_colours = $colours->RecordCount();
// end Recordset
Here's the syntax of selectLimit based on the adodb-php source at Github
SelectLimit($sql, $nrows = -1, $offset = -1, $inputarr = false, $secs = 0)
When you didn't supply $nrows parameter, it will default to -1, the function will assign 18446744073709551615 as $nrows value when the parameter is -1 (which is the default value when you don't supply that parameter).
18446744073709551615 is the maximum unsigned 64 bit integer which used to retrieve all rows
I tried running this sql
UPDATE table
SET read = 1
WHERE col1_id = 2
AND col3_id = 1;
and it return an error (error in your sql syntax)
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read = 1 WHERE team_notification_id = 2 AND listener_id = 1' at line 1
But when I used thesame WHERE and AND :), in a SELECT statement no error was returned.
SELECT *
FROM read
WHERE col1_id = 2
AND col3_id = 1
please what did I do wrong, I am no seeing it.
After keyword UPDATE there should be a table name. If your table has actually name table (that's a bit strange) you should escape it to let MySQL know it's not a keyword "table" but actual table name.
The same thing is about read.
Both table and read are MySQL keywords (the full list is here http://dev.mysql.com/doc/refman/5.7/en/keywords.html) so if it's your actual table and column names then you should escape it.
Better to escape all table and column names to prevent issues like that.
Escaping in MySQL is done with backtick symbol ` so your query will looks like:
UPDATE `table`
SET `read` = 1
WHERE `col1_id` = 2
AND `col3_id` = 1;
UPDATE tablename
SET `field_name` = 1
WHERE col1_id = 2
AND col3_id = 1;
UPDATE table
SET [read] = 1
WHERE col1_id = 2
AND col3_id = 1;
I have a query that will display columns with duplicate or with more than 1 values.I can display it using sql
select date_created,loan_id,count(1) as cnt
from collections
group by date_created,loan_id
having count(1)>1;
I want that to convert to Doctrine 1 query,I tried
public function getDuplicateDatePayment() {
$q = $this->createQuery('c')
->select('c.date_created,c.loan_id,c.count(1) as cnt')
->groupBy('c.date_created','c.loan_id')
->having('c.count(1) > 1');
return $q->execute();
}
But it only return errors.Any Idea on how to correctly convert the said working sql into a doctrine 1 query?
SQLSTATE[42000]: Syntax error or access violation: 1630 FUNCTION c.count does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual. Failing Query: "SELECT c.id AS c__id, c.date_created AS c__date_created, c.loan_id AS c__loan_id, c.count(1) AS c__0, c.count(1) AS c__0 FROM collections c GROUP BY c.date_created HAVING c.count(1) > 1"
I hope the problem may be with count. Try the following
public function getDuplicateDatePayment() {
$q = $this->createQuery('c')
->select('c.date_created,c.loan_id,count(c.1) as cnt')
->groupBy('c.date_created','c.loan_id')
->having('c.count(1) > 1');
return $q->execute();
}
Am I blind, or what is wrong with my query?
select
STRCMP( message, 'LogMessage') = 1
from
LogEntries;
works fine. However
select
IF STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF
from
LogEntries;
returns:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF from
LogEntries' at line 2
What is wrong with this statement?
You might want to use CASE WHEN:
SELECT
CASE WHEN STRCMP( message, 'LogMessage') = 1 THEN 'bla' END AS your_column
FROM
LogEntries;
when the condition is true it will return 'bla' otherwise, since there's no else part, it will return NULL.