MySQL AND keyword issue - mysql

I have a query which is behaving strange...
Firstly, here is a query to get all PMs whether or not they've been read or deleted for the user ID 1:
SELECT * FROM `pms` WHERE `toid` = '1'
This returns 3 rows as expected. Next, let's see if I can get only unread messages for this user:
SELECT * FROM `pms` WHERE `toid` = '1' AND `read` = '0'
This returns 2 rows as expected. Let's see if I can get any read and unread messages which have been binned:
SELECT * FROM `pms` WHERE `toid` = '1' AND `binned` = '0'
This returns 2 rows as expected.
The query which I need to run is getting all unread and not binned messages for a specified user id. To do this, I am doing this:
SELECT * FROM `pms` WHERE `toid` = '1' AND `read` = '0' AND `binned` = '0'
However, it should be returning 1 row as I know in the database there is a message with toid as 1, read as 0 and binned as 0 but for some reason this query above is returning 0 rows...
Why is this?
UPDATE
Here is a screenshot of my table structure as seen in Sequel Pro:
Here is a screenshot of the data inside the table as seen in Sequel Pro:
As you can see there is definitely 1 record with toid as 1, read as 0 and binned as 0.
UPDATE 2
The reason these are ENUM is because I'm wishing to store a boolean value in MySQL. I do this by enforcing the column to be either a '1' or a '0' and making it default to '0' as well. If anyone has a better way of storing boolean values in MySQL then I'd love to learn.
Secondly, here is my PHP function inside of my User.class.php file which is getting the unread count using this SQL. This function is returning 0 when it should be returning 1. The $this->getUserId() is returning 1 as that is the current user I am using:
public function getUnreadCount()
{
global $database;
$sql = "SELECT * FROM `pms` WHERE `toid` = '".$this->getUserID()."' AND `read` = '0' AND 'binned' = '0'";
$query = $database->query($sql);
$count = $database->count($query);
return $count;
}
Thanks for the help so far but I still cannot work out why this isn't working. I'm using the read in the query adding backticks to prevent MySQL from using it as a keyword.
I bet its something really obvious I'm missing...

James, I think the problem might have to do with how the table was populated.
Since the "read" and "binned" columns' datatypes are ENUMs, you probably have to either set the correct default value ('0' or '1') or always provide a valid value when inserting a row into this table. In other words, you can't omit a value for either the "read" or "binned" columns when inserting a "pms"-row.
In other words, if your "pms" table is set up as follows, without defaults:
create table pms (
toid int,
`read` ENUM('0','1') ,
binned ENUM('0','1')
);
then you have to insert fully specified row-values like so:
insert into pms (toid, `read`, binned) values
(1, '0', '0'),
(1, '0', '1'),
(1, '1', '0'),
(1, '1', '1')
;
and avoid inserting sparse data like this:
insert into pms (toid) values (1);
insert into pms (toid, binned) values (1, '1');
insert into pms (toid, `read`) values (1, '1');
insert into pms (toid, `read`, binned) values (1, '1', '1');
Providing the correct default enum-value for those columns would also solve this issue:
create table pms (
toid int,
`read` ENUM('0','1') default '0',
binned ENUM('0','1') default '0'
);
I've set up a sqlfiddle to illustrate.

if your columns are integers try doing this
SELECT * FROM `pms` WHERE `toid` = 1 AND `read` = 0 AND `binned` = 0
EDIT:
it should be your columns to be integers like that in this demo.
SQLFIDDLE DEMO
or to be enum with values as strings like here
SELECT * FROM `pms`
WHERE `toid` = 1 AND `read` = '0' AND `binned` = '0'
sqllfiddle demo

Try to test if you have set your variables correctly. I suggest by testing if you get the right results when querying for just one variable.:
SELECT * FROM `pms` WHERE `toid` = '1'; -- 3;
SELECT * FROM `pms` WHERE `read` = '0'; -- 4;
SELECT * FROM `pms` WHERE `binned` = '0'; -- 4;
Classic mistakes would be that you have used integer values instead of string (ENUM) values or have substituted the zero for an null.
SQL FIDDLE DEMO

Wow haha I've just found why its not been returning the rows.
I'd mistakenly used single quotes instead of backticks in my PHP implementation of the SQL query...
So my query was actually:
$sql = "SELECT * FROM `pms` WHERE `toid` = '".$this->getUserID()."' AND `read` = '0' AND 'binned' = '0'";
When it should've been:
$sql = "SELECT * FROM `pms` WHERE `toid` = '".$this->getUserID()."' AND `read` = '0' AND `binned` = '0'";`
As you can see, near the end of the query for binned I had mistakenly used single quotes.
Can you believe it was that simple?
Just out of interest, how do you think I should be storing boolean values in MySQL?

Related

Laravel: is there a way to execute fast bulk update?

I came across a method of fast bulk update in mySQL recently:
The slow way:
UPDATE `example` SET `name` = 'Mary', `age` = '20' WHERE id = 2;
UPDATE `example` SET `name` = 'Nancy', `age` = '30' WHERE id = 4;
UPDATE `example` SET `name` = 'Oliver', `age` = '40' WHERE id = 5;
The fast way:
UPDATE `example` SET
`name` = ELT(FIELD(id,2,4,5),'Mary','Nancy','Oliver') ,
`age` = ELT(FIELD(id,2,4,5),'20','30','40')
WHERE id IN (2,4,5);
According to my google searching, the fast way is 50 times faster at large scale than the slow way, so I am wondering whether Laravel support this type of bulk update without the need to execute raw sql generated by myself.
Also if anyone is familiar with mysql and can tell me is this really that much faster or not, and if there is any scenario this method is actually worse I will be very graceful.
Edit(as requested in comment):
My current Laravel code would work like this for update:
foreach ($modelList as $model) {
$model->setConnection($connection);
$model->save();
}
What I would like for bulk update is
DB::connection($connection)->table($table)->bulkUpdate($models);
As I know currently Laravel already supports bulk insert as follows:
foreach ($models as $model) {
$attributes = $model->getAttributes();
$params[] = $attributes;
}
DB::connection($connection)->table($table)->insert($params);
which will generate one insert statement with multiple records instead of multiple insert statements.
If there's no solution, you can still do it raw with prepared statements:
DB::update("
UPDATE `example` SET
`name` = ELT(FIELD(id,2,4,5),?,?,?) ,
`age` = ELT(FIELD(id,2,4,5),?,?,?)
WHERE id IN (?,?,?)
", [
'Mary', 'Nancy', 'Oliver',
'20', '30', '40',
2, 4, 5
]);
Technically, this should also work. Haven't checked it yet:
$sql = DB::table('users')->update([
['name' => DB::raw("ELT(FIELD(id,2,4,5),'Mary','Nancy','Oliver')")],
['age' => DB::raw("ELT(FIELD(id,2,4,5),'20','30','40')")]
])
->whereIn('id', [ 2, 4, 5 ]);
Another approach:
INSERT INTO t
(name, age, id)
VALUES
('Mary', 20, 2),
('Nancy', 30, 4),
('Oliver', 40, 5)
ON DUPLICATE KEY UPDATE name = VALUES(name);
Assuming the ids are already in the table and it has PRIMARY KEY(id), there would be any INSERTs, only UPDATEs -- which is what you want.
The name = VALUES(name) is a dummy no-op. It needs to change to name = NEW.name in MySQL 8.0.
From Comment...
If you must not INSERT "new" rows, then consider this:
INSERT INTO tmp
(name, age, id)
VALUES
('Mary', 20, 2),
('Nancy', 30, 4),
('Oliver', 40, 5);
UPDATE t
JOIN tmp USING(name);
(I am not sure of the syntax; play around with the "multi-table update".)
This second 'solution' requires creating the tmp table and/or having it already available (plus possibly a TRUNCATE).
UPDATE student set roll='12',fee=fee-10 where roll='15'
Use this example for your code. This is easier and fastest way to support any platform for mysql.

MYSQL - INSERT INTO using WHERE condition of Table 1

after reading all the "INSERT INTO" posts as well as the documentation, I am still unsure whether what I want is feasible or not.
I want to change the UPDATE below into an INSERT INTO, because I have many 100-thousands of them (speed issues):
UPDATE city c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
WHERE
p17t = 30 AND p17l = '30';
or
UPDATE city c
SET
c.g1h = '0',
c.g1q = '0',
c.g1k = '0',
c.g1w = '0',
c.g1s = '0'
WHERE
p1t = 1
AND p1l = '1';
However, my best solutions do not work:
INSERT INTO city (g17h, g17q, g17k, g17w, g17s)
SELECT
'3196504',
'2593487',
'0',
'0',
'0'
FROM valuestoretab
WHERE
p17t = 30
AND p17l = '30';
This is of course because my WHERE condition can only be satisfied in the first table (city) but not in the second one which is just a table of values, whereas the city table is a data set where each id has particular values for p17t, p17l and so on.
For clarification:
The first table (city) looks like
cityid1, ownerid1, islandid1, p17t, p17l
cityid2, ownerid2, islandid2, p17t, p17l
with different values for each row for p17t and p17l.
So, my questions would be:
1. Is it at all possible to write a INSERT-Query with a WHERE condition for the table that is being inserted into?
2. If no, do I have to stick to my UPDATE or is there another (fast!) solution?
Thanks to the community!
litotes
To give a quick answer insert with a select statement is possible
Insert INTO MyTable (Val1, Val2)
Select
SomeValue,
AnotherValue
From MyOtherTable
Where Date = Getdate()
On the other hand, you can also update many records like in the following example:
Update t1
Set
t1.Val1 = t2.SomeValue,
t1.Val2 = t2.AnotherValue
From MyTable t1
Inner join MyOtherTable t2 ON t1.PK = t2.FK
where t2.Date = getdate()
EDIT:
When i read the following query, I presume p17t and p17l are from the valuestoretab.
UPDATE city c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
WHERE
p17t = 30 AND p17l = '30';
==> changed this into:
UPDATE c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
FROM City c, ValueStoreTab v
WHERE
c.SomeCol = v.SomeCol -- Here, your relation must exist!
AND v.p17t = 30 AND v.p17l = '30';

Mysql - How can I select rows based on two criterias

I'm trying to run this query, to select all the rows that have status = 1:
SELECT
acc.id,
acc.user_id,
acc.type,
acc.account,
acc.`status`,
acc.paid,
acc.`password`, CAST(AES_DECRYPT( BASE64_DECODE( `password` ), 'encryption-key') AS CHAR)
FROM acc
WHERE status = status = '1'
However, it returns all the tables, it does't select only the rows that have status = 1
It's probably a syntax error. I do need this
`acc.`password`, CAST(AES_DECRYPT( BASE64_DECODE( `password` ), 'encryption-key') AS CHAR)`
part so I can decrypt the passwords.
What did I do wrong?
You don't need to append acc. to all the fields given you're only retrieving data from one table.
You had and extra status attached to your condition
Should be:
SELECT `id`,
`user_id`,
`type`,
`account`,
`status`,
`paid`,
`password`,
CAST(AES_DECRYPT(BASE64_DECODE(`password`),'encryption-key') AS CHAR)
FROM acc
WHERE status = '1'

Mysql Case - 2 conditions need same update value

I am trying to use a mysql case query to update multiple rows in a table. I have some cases which need to update the row with the same value. I was wondering whether it is possible to put all of these into one case or whether I have to create a new 'WHEN' for each?
Below is an example of what I am trying to accomplish but it obviously isn't the correct way to do this because I get an error.
UPDATE `groups` SET `status` = CASE `group_id`
WHEN 32 OR WHEN 33 THEN '1'
WHEN 31 THEN '2'
END
Is it possible to do something like that?
Thanks
I think you want
UPDATE groups SET status = CASE
WHEN group_id = 32 OR group_id = 33 THEN '1'
WHEN group_id = 31 THEN '2'
END
Edit You can use operators like BETWEEN. For example
UPDATE groups SET status = CASE
WHEN group_id BETWEEN 32 AND 33 THEN '1'
WHEN group_id BETWEEN 30 AND 31 THEN '2'
END
TRY
UPDATE `tablename` SET `status`= IF('group_id=31',2,1)
EDIT
UPDATE tableName SET `status` = IF( group_id IN (31, 32), 2, 1 ) WHERE section_id=1
OR
UPDATE tableName SET `status` = IF( group_id ANY (31, 32), 2, 1 ) WHERE section_id=1
running successfully on my table..what error u facing??
other syntax
UPDATE `tableName` SET `group_id` = CASE
WHEN group_id IN (31,32) THEN 1
WHEN group_id IN (33,34) THEN 2
END

Inserting NULL into column?

I'm trying to figure out how I can insert a NULL value into a column of newly inserted row.
I'm working on "post icons" which will display a small icon for the thread on the thread list. The icons use RADIO buttons in the form. By default it is set to No Icon. I was wondering what to do for the value of that radio button to make it null? I tried setting value as NULL but it just inserts the word "NULL" into the database.
$thread_sql = "
INSERT INTO forum_threads (
user_id,
forum_id,
thread_postdate,
thread_lastpost,
thread_title,
thread_description,
thread_icon
) VALUES (
'$_SESSION[user_id]',
'$_GET[f]',
'$date',
'$date',
'$_POST[topictitle]',
'$_POST[topicdescription]',
'$_POST[posticon]'
)
";
$thread_query = #mysqli_query ($db_connect, $thread_sql);
With MySQL:
insert into your_table (nulled_column) values (null);
If you are getting a 'NULL' literal instead of a null value, it's probably because you are not issuing the right command from your client (PHP, C#, Java program). You will need to share the code you are using for your insertion in order to get more help.
UPDATE:
According to your recently edited question, just get rid of the '' when you are inserting NULL values. If you use '', then you'll get 'NULL' literals inserted as you mentioned.
I would also suggest you use PreparedStatements in MySQL in order to avoid SQL injection attacks.
Just do something like this (PDO):
$stmt = $db->prepare('INSERT INTO foo(bar) values (?);');
$stmt->execute(array($value=='NULL'? NULL,$value));
Or with mysql:
mysql_query('INSERT INTO foo(bar)
values ('.($value=='NULL'? 'NULL',"'".mysql_real_escape_string($value)."'").')';
This works. Had to use an IF
if (isset($_POST['submit'])) {
$thread_sql = "
INSERT INTO forum_threads (
user_id,
forum_id,
thread_postdate,
thread_lastpost,
thread_title,
thread_description,
thread_icon
) VALUES (
'$_SESSION[user_id]',
'$_GET[f]',
'$date',
'$date',
'$_POST[topictitle]',
'$_POST[topicdescription]',
IF('$_POST[posticon]'='NULL',NULL,'$_POST[posticon]')
)
";
$thread_query = #mysqli_query ($db_connect, $thread_sql);