Please how can I update different columns base on different and specific conditions. For example:
UPDATE table
SET col1 = val1 WHERE col1 > 2
SET col2 = val2 WHERE col2 > 1
Is is possible to write an SQL UPDATE statement like this
Where different columns will be updated base on separate conditions?
Use case:
UPDATE table
SET col1 = (CASE WHEN col1 > 2 THEN val1 ELSE col1 END),
col2 = (CASE WHEN col2 > 1 THEN val2 ELSE col2 END);
You can also add WHERE col1 > 2 or col2 > 1 so MySQL does not attempt to update all rows.
Related
I am using Postgres with PHP, I have two dependent queries as below:
INSERT INTO tab2
SELECT * FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL;
DELETE FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL ;
I want these both to run or none of them. So I created below code block:
DO $$
BEGIN
INSERT INTO tab2
SELECT * FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL ;
DELETE FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL ;
END$$
But this is not working and giving below error:
ERROR: bind message supplies 1 parameters, but prepared statement "" requires 0
PHP Code:
$result = pg_query_params($pg_con_write,$query_PG,array($Param1));
I think I can't use Bind Parameters in this way... So please suggest the best possible way to do so...
I think $$ is the issue, you should rewrite your query like:
BEGIN;
INSERT INTO tab2
SELECT * FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL;
DELETE FROM tab1 WHERE col1 = $1 AND col2 IS NOT NULL;
COMMIT;
I have two columns that store values(point values).
How do I select where my given number is between the values in the two columns ?
I hope you can get it by doing this:
Lets your number is $your_number and table name is your_table
So your query would be:
SELECT * FROM your_table WHERE $your_number BETWEEN col1 AND col2;
Or if it is ensured that col1 < col2
SELECT * FROM your_table WHERE $your_number >= col1 AND $your_number <= col2;
I have a mysql query that selects the records where col1 and col2 = 4 and col3 or col4 = 0
SELECT * FROM table WHERE 4 IN(col1, col2) && (col3 = 0 || col4 = 0)
The or is not working. It returns zero rows and I know that there are 4
&& and || in SQL are AND and OR.
Rewrite that to:
SELECT * FROM table WHERE (col1 = 4 AND col2 = 4) AND (col3 = 0 OR col4 = 0)
You can't use IN for column names. You can use IN like this:
WHERE col3 IN('4','a','another valid value'),
which is equals to WHERE (col3 = '4' OR col3 = 'a' OR ...)
Try like below instead cause per your posted code 4 IN (col1, col2) will be col1=4 or col2 = 4 whereas you want it to be and condition
Per last comment update; below query will get you exactly those records
There is a total of 4 rows where column1 or column2 = 4. of those
rows I only want to return the rows where col3 or col4 = zero
SELECT *
FROM test4
WHERE (col1 = 4 or col2 = 4)
and (col3 = 0 or col4 = 0)
I was reading How to update multiple rows with a single query but i have a question i want to know if you can help me.
I want to update multiple columns and rows with a single query but i need them to update like this:
If column1 and column2 (in all rows) == column1 and column2 (in specific row1) then update value in column3 (in all rows). if not equal then value Whatever but if > or < then value Whatever.
Example :
Column 1 ---- Column2 ----- Column3
Row1 2 3
Row2 2 3 (then) Value1
Row3 2 2 (then) Value2
Row4 2 3 (then) Value1
Many thanks
I think what you're saying is that you want to update rows based on matching to the values in a specific row. We can achieve this by doing a left outer join. If the values match then the join succeeds, so 'row1Match' will not be null. If the values don't match then 'row1Match' will be null. We can then use this to drive the update.
If we assume that the table (which I've called MyTable) has an ID column, and that 'Row1' in your example has an ID of 1, then you want something like this:
UPDATE toUpdate
SET Column3 = (CASE WHEN row1Match IS NOT NULL THEN 'Value1' ELSE 'Value2' END)
FROM MyTable toUpdate
LEFT OUTER JOIN MyTable row1Match ON row1Match.Column1 = toUpdate.Column1
AND row1Match.Column2 = toUpdate.Column2
WHERE row1Match.ID = 1
How do I know which column matched if I have two LIKE clauses for two different columns and eventually one of the two columns contains the needle.
you could do something like:
SELECT 1 AS `match`, * FROM `table` WHERE col1 LIKE 'needle%'
UNION
SELECT 2 AS `match`, * FROM `table` WHERE col2 LIKE 'needle%'
and use match to know
Note: this could probably be written with a IF()
SELECT
IF(col1 LIKE 'needle%' AND col2 LIKE 'needle%', 0, IF(col1 LIKE 'needle%', 1, 2)
AS match,
*
FROM
table
WHERE
col1 LIKE 'needle%'
OR
col2 LIKE 'needle%'
match teklls you which colums hit:
0 = both
1 = match on col1
2 = match on col2
You can use your PHP section to check which one matched.
if (stristr($result['col1'], $queryvar) === true) // col1 matched