What is the best way to merge two mysqli query's as one query
TABLE SERVER_JOINS
ID DEFAULT SERVER_ID MEMBER_ID
---------------------------------------
1 0 1 57
2 0 52 57
3 0 22 57
4 1 35 57
Only one row must have default as 1
By clicking on a link i want to change the default value
mysqli_query($database->connection,"UPDATE `server_joins` SET
`default` = '0' WHERE `default` = '1' AND `member_id` = '$session->u_id'");
mysqli_query($database->connection,"UPDATE `server_joins` SET
`default` = '1' WHERE `server_id`= '$id' AND `member_id` = '$session->u_id'");
I think you want to schieve something like that:
mysqli_query($database->connection,"UPDATE `server_joins`
SET `default` = '1'
WHERE `default`= '0' AND `server_id`= '$id' AND `member_id` = '$session->u_id'");
Use an if() function or a conditional case expression in the set clause depending on the value of the server field to decide whether default is to be set to 0 or 1.
UPDATE `server_joins`
SET `default` = if(`server_id`= $id, 1, 0)
WHERE `member_id` = $session->u_id
Couple of notes:
Your code is likely to be vulnerable to sql injection attack. Consider using prepared statements with parameters.
If you have numeric values in a field, then do not pass the values as string. Mysql has to convert the values on the flight.
Related
I've included a DB-fiddle, and you can adjust the input parameter accordingly. This returns how I would expect it to, and differs from the results I am seeing in PDO.
I have the following minified table-view and query:
CREATE TABLE `tagged` {
`tag` SMALLINT(5) UNSIGNED NOT NULL
}
Table has an assortment of values, but you can use 1-10 for tags in the DB:
INSERT INTO tagged (tag) VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)
query:
SELECT tagged.tag,
(#t := :tag),
#t AS temp_var,
(#t IS NULL OR FIND_IN_SET(tagged.tag, #t) > 0) AS is_match
FROM tagged
HAVING is_match = 1
LIMIT 150
This seems well and good when run in a client, command line, jdbc, etc. If I put in an input of '' or NULL, I get all results. Similarly an input of '1' yields only tags of 1, and an input of '1,4' would retrieve all tags with 1 or 4.
The way the query restricts these results is via the is_match = 1 in the HAVING clause. When run with PDO, the parameter seems to bind correctly but it completely ignores the condition in the clause:
Array
(
[0] => stdClass Object
(
[tag] => 3
[(#t := ?)] => 1,4
[temp_var] => 1,4
[is_match] => 0 ## should not have been returned
)
[1] => stdClass Object
(
[tag] => 4
[(#t := ?)] => 1,4
[temp_var] => 1,4
[is_match] => 1
)
PHP code used to run this (simplified):
$conn = /* pdo connection object */;
$stmt = $conn->prepare(DB::queryOf('test')); //uses our above query from a file
$stmt->bindValue(':tag', $args['tag'], PDO::PARAM_STR); //hardcode binding '1,4'
$stmt->execute(); //also tried plain #execute($args)
return $stmt->fetchAll(PDO::FETCH_OBJ);
Is there something I'm missing? I am binding a direct string parameter, and it seems the temporary variable is there and set correctly. Why is PDO returning the results for elements where is_match = 0?
I believe this behavior is dependent on the RDBMS being used.
In the absence of the GROUP BY clause, it seems that in some circumstances, the entire result can be considered as "one group". Because one row in the results satisfies the HAVING condition, all shall pass.
Additional reading:
Use of HAVING without GROUP BY in SQL queries
HAVING without GROUP BY
p.s. I don't think the > 0 is necessary.
I suppose I'd write your query something like this:
SELECT tag,
#t := '1,4' AS temp_var,
1 AS is_match
FROM tagged
WHERE #t IS NULL OR FIND_IN_SET(tag, #t)
LIMIT 150;
I have created two tables subscription_pending and subscription_complete
subscription_complete contain
id mag1 mag2 mag3 mag4
4 100 0 100 0
subscription_pending contain
id mag1 mag2 mag3 mag4
4 100
I have insert value by following command
$final_q= "INSERT INTO `subscription_complete` (`id`, `mag1`, `mag2`, `mag3`, `mag4`) VALUES ('".$row_q['id']."','".$row_q['mag1']."','".$row_q['mag2']."','".$row_q['mag3']."','".$row_q['mag4']."'
i want to update the subscription_complete table without replacing its value i.e. 100 for that i hvae write following query
$final_q1= "update subscription_complete set mag1='".$row_q['mag1']."',mag2='".$row_q['mag2']."',mag3='".$row_q['mag3']."',mag4='".$row_q['mag4']."' where id='".$row_q['id']."'";
can you tell me how to set 100 value in remaining column where the value is 0 ?
Thank you in advance !!
This assumes the empty fields in subscription_pending contain NULL, not empty strings.
UPDATE subscription_complete AS c
JOIN subscription_pending AS p ON p.id = c.id
SET c.mag1 = IFNULL(p.mag1, c.mag1),
c.mag2 = IFNULL(p.mag2, c.mag2),
c.mag3 = IFNULL(p.mag3, c.mag3),
c.mag4 = IFNULL(p.mag4, c.mag4)
If they're actually empty strings, use IF(p.magX = '', c.magX, p.magX) instead of the IFNULL test.
I have face a problem with my UPDATE Query in Mysql-
mysql_query( INSERT INTO `cost`
SET `cat_id` = '18', `feature_id` = '77', `type_id` = '5',
`cost_from` = '600', `cost_to` = '800'
WHERE `type_id` = 5 && `cat_id` = 18 && `feature_id` = 77");
type_id = 5 is not in my current table where i want to SET my new values.
The result is Unsuccessful.
I tried also an UPDATE query for the same
mysql_query( Update `cost`
SET `cat_id` = '18', `feature_id` = '77', `type_id` = '5',
`cost_from` = '600', `cost_to` = '800'
WHERE `type_id` = 5 && `cat_id` = 18 && `feature_id` = 77");
What should i do to insert and update values at the same time?
Note:
You don't use WHERE and SET on an INSERT query.
Make sure you have a table named cost along with the columns cat_id, feature_id, cost_from, cost_to, type_id.
You're trying to update the cost table columns with the ones you are looking for. Doesn't seem right.
Your first and second given sample codes have the same format, you just changed the first word, INSERT and UPDATE. Doesn't work that way.
You can, I think the recommendation of all people that would see your code, create better queries using mysql_* prepared statements.
A simple example of an INSERT query would look like this:
/* PREPARE YOUR QUERY */
$stmt = $con->prepare("INSERT INTO cost (cat_id, feature_id, type_id, cost_from, cost_to) VALUES (?,?,?,?,?)");
/* BIND PARAMETER TO THE QUERY. REPLACE NECESSARY VALUE OR VARIABLE */
$stmt->bind_param('iiiss', $catid,$featureid,$typeid,$costfrom,$costto);
$stmt->execute(); /* EXECUTE QUERY */
A simple example of an UPDATE query:
/* PREPARE YOUR QUERY */
$stmt = $con->prepare("UPDATE cost SET cat_id=?, feature_id=?, type_id=?, cost_from=?, cost_to=? WHERE id=?");
/* BIND PARAMETER TO THE QUERY. REPLACE NECESSARY VALUE OR VARIABLE */
$stmt->bind_param('iiissi', $catid,$featureid,$typeid,$costfrom,$costto,$id);
$stmt->execute(); /* EXECUTE QUERY */
First of all, you cannot use 'set ' with ' insert into' , and if you want to insert and update queries at the same time, then your best bet is using transaction . (you have to enclose insert and update queries in a transaction) , you cannot enclose both on a single sql statement..
This post might help you
SQL Update,Delete And Insert In Same Time
My MySQL statement (wich I used 4 days to find a working one) SHOULD take 1 given value and check 3 different columns if they are filled or NULL. If the column got a value, it goes to the next column. If that column is NULL, it shall write the given value into that cell.
Unfortunately my statement is able to fetch that column, but when I try to SET the value, it is always set to 0. When i try to +1 the existing value it works with no problems.
Statement:
UPDATE `license` SET
`hardwareid1` = IF( `hardwareid1` = 123,`hardwareid1` = 987, `hardwareid1`) ,
`hardwareid2` = IF( `hardwareid2` = 123, `hardwareid2` = 987, `hardwareid2` )
WHERE `code` = 99
Where the = 123 is, in production I'll use IS NULL the 987 stands for the example value from "$value"
So if hardwareid1 is really 123 it doestn write 987 but 0 instead.
Please help
UPDATE license
SET hardwareID3 = CASE WHEN hardwareID1 IS NOT NULL AND
hardwareID2 IS NOT NULL AND
hardwareID3 IS NULL
THEN 987
ELSE hardwareID3 END,
hardwareID2 = CASE WHEN hardwareID1 IS NOT NULL AND
hardwareID2 IS NULL
THEN 987
ELSE hardwareID2 END,
hardwareID1 = CASE WHEN hardwareID1 IS NULL
THEN 987
ELSE hardwareID1 END
WHERE code = 99
SQLFiddle Demo
What the query does is when hardwareID1 IS NULL the new value 987 will be inserted on the column. At the same time, the value of hardwareID2 will also be changed if hardwareID1 has a value and hardwareID2 is null. Otherwise, all their value will retain.
I don't really know, but shouldn't your SQL look more like this?
`hardwareid1`=CASE
WHEN `hardwareid1`= NULL THEN set_to_xy
so I have this code:
$query = "REPLACE INTO {tbl} SET a_id = %d, p_id = %d, comment = '%s'";
db_query($query,1321,-1,"lolo");
but then when I execute it, instead of inserting -1, it inserted 0 instead....
using INSERT does the same thing...
how can I set this to -1?
using drupal 6
also the p_id column is just an integer in mysql
I would check that p_id is not flagged as unsigned integer