Mysql Case - 2 conditions need same update value - mysql

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

Related

Update and append to all items in a specific column

How do I append a character to all items `purchase_id' here is a manual example of what I want...
SELECT *
FROM `loadable_link`
WHERE `product_sku` = '2101-R'
ORDER BY `customer_id` DESC
Then select from purchased_id and append a '0' to all purchased ID's
UPDATE `loadable_link` SET `purchased_id` = '11165690'
WHERE `loadable_link`.`purchased_id` = 1116569;
You can update the table according to the condition in the original select statement.
If purchase_id is a number, you can multiply it by 10:
UPDATE `loadable_link`
SET `purchase_id` = `purchase_id` * 10
WHERE `product_sku` = '2101-R'
If purchase_id is a string, you can concatenate a 0 to it:
UPDATE `loadable_link`
SET `purchase_id` = CONCAT(`purchase_id`, '0')
WHERE `product_sku` = '2101-R'
UPDATE `loadable_link`
SET `purchased_id` = CONCAT(`purchased_id`, "0")
WHERE `product_sku` = "2101-R";
This can be achieved in one UPDATE query, take the original value of each row and CONCAT() to append a 0 to the end of the existing purchase_id.

how can i define 2x WHERE

current query:
SELECT * FROM `invoices2` WHERE `status` = 2
I need to select status 1 and status 2, how can I do it?
I already tried things like 2 AND 1 or WHERE 'status' = 2 AND WHERE 'status' = 1
Thank you guys!
All you need is:
WHERE status IN (1, 2)
To be clear, this is equivalent to:
WHERE status = 1 OR status = 2;
You can have very complex expressions in the WHERE clause.
You can also do:
SELECT * FROM `invoices2` WHERE `status` = 1 OR `status` = 2
In single query you always have only one WHERE keyword and then you can extend it by adding OR, AND, NOT.
If they are in different rows you need and for more Status values its own extsts
SELECT * FROM `invoices2`
WHERE `status` = 2 AND EXISTS (SELECT 1 FROM `invoices2` WHERE `status` = 1)

SQLite Query SET variable on a single row

I have a table called "allarmi_ingressi" in SQLite, with a lot of rows in it.
I want to create a query that changes the variable on my column "visto" to 1, if "visto=0", and to 0, if "visto=1".
This is what i made:
UPDATE allarmi_ingressi SET visto = '1' WHERE visto = '0'
Of course this modify every row in the column "visto";
I want to know if it's possible to modify it "selecting" it by the primary key, in my case "id_allarme".
In a SELECT query, you would use the WHERE clause to find rows with a specific id_allarme value.
The same WHERE clause can be used with UPDATE:
UPDATE allarmi_ingressi
SET visto = 1 - visto
WHERE id_allarme = ?;
Use CASE Expression
Query
update allarmi_ingressi
set visto = (
case visto when '1' then '0'
when '0' then '1'
else visto end
)
where id_allarme = __; -- id here

MySQL query Find and Replace in Specific Columns in a Table?

I need to set all values in certain columns...to 1 (where they are now NULL)
Can anyone help out with a little assist on this SQL query/command syntax?
I need to replace several columns where the SchoolID is 184 with a 1, something like this?
SELECT * FROM tblMembers WHERE SchoolID SET column = '1';
You need to use an UPDATE statement:
UPDATE tblMembers
SET column = '1'
WHERE SchoolID = '184'
You can set multiple columns at the same time:
UPDATE tblMembers
SET column = '1', column2 = 'somethingelse', column3 = 'somethingelse'
WHERE SchoolID = '184'

MySQL AND keyword issue

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?