in PHP pdo how can i use an incremental identifier - php

I have the following challenge:
a "Tasks" table:
tasksId int
listId int
taskOrder float
in case i want to move all the tasks from list 2 to list 3 i would do something like:
// pseodo code //
#lastTaskOrder = last task order in list 3
loop - {
UPDATE tasks SET taskOrder = #lastTaskOrder + 1, listId = 3 WHERE listId = 2;
#lastTaskOrder++
}
thus the taskOrder stays unique.
in case i want to move all the tasks from list 2 to the beginning of list 3 i would do something like:
// pseodo code //
#firstTaskOrder = first task order in list 3
#delta = #firstTaskOrder / #numberOfTasksToMove
UPDATE tasks SET taskOrder = #firstTaskOrder + #delta, listId = 3 WHERE listId = 2;
#firstTaskOrder = #firstTaskOrder + #delta
is it possible with mySQL + PDO + PHP?

Short answer: yes.
Longer answer involves some code. To update your list_ids and increment them based on the highest current value in the old list, I had to use a subquery with a window function:
UPDATE tasks
SET list_id = :toList,
task_order =
(SELECT MAX(task_order) from tasks where list_id = :toList)
+ t2.task_sort_order
FROM ( SELECT task_id,
row_number() OVER (PARTITION BY list_id order by task_order)
AS task_sort_order
FROM tasks ) t2
WHERE tasks.task_id = t2.task_id AND tasks.list_id = :fromList
Edit This is heavily edited from the first version. I've thrown away all the PHP in favor of just showing the SQL. I changed the column names because my version of Postgres was complaining about the camel-case names.

this proved to be easier than i thought it would be.
It took me 2 hours but i got it figured out:
SET #start=100;
SET #delta=1.5;
UPDATE tasks SET taskOrder = #start:= (#start+#delta), listId = 3
WHERE listId=2
ORDER BY taskOrder
I PDOed this query with the correct values

Related

Creating a next and previous buttons

I'm trying to do a next and previous button based on records from database but i'm not really sure how to do it and which would be the best way.
The way that i was thinking to do it
would be to have a field in the table named position.
and the query would look like this:
SELECT * FROM t1 WHERE step = 1
then in the php i would have something like this
$step = $step + 1 // for the next button
if($step === 1) {
$step = 1;
} else {
$step = $step - 1;
}
But if i would delete a record then my position would be out of sync with the math and i won't be able to do + or - on step field because i would find the record.
Which means everytime i would delete something i would need to do a new increment on that field to keep it synced which might later become a problem.
How should i handle it ?
You can add a row number to a query like this:
select #qposn:=#qposn+1 posn, a.* from sometable a, (select #qposn:=0) x
You can select a specific row, in this case the 2nd row, like this:
select * from (
select #qposn:=#qposn+1 posn, a.* from sometable a, (select #qposn:=0) x
) x where posn=2
But be aware that if the data in the table changes the numbering will change also. If someone deletes the first record in the table then the data that used to be row 2 will now be row 1 and so paging may skip or repeat rows.
If your table is relatively small you might consider caching the table in a javascript array and then paging through the array.

PHP/MySQL make top 10 out of selection

A user can input it's preferences to find other users.
Now based on that input, I'd like to get the top 10 best matches to the preferences.
What I thought is:
1) Create a select statement that resolves users preferences
if ($stmt = $mysqli->prepare("SELECT sex FROM ledenvoorkeuren WHERE userid = you"))
$stmt->bind_result($ownsex);
2) Create a select statement that checks all users except for yourself
if ($stmt = $mysqli->prepare("SELECT sex FROM ledenvoorkeuren WHERE userid <> you"))
$stmt->bind_result($othersex);
3) Match select statement 1 with select statement 2
while ($stmt->fetch()) {
$match = 0;
if ($ownsex == $othersex) {
$match = $match + 10;
}
// check next preference
4) Start with a variable with value 0, if preference matches -> variable + 10%
Problem is, I can do this for all members, but how can I then select the top 10???
I think I need to do this in the SQL statement, but I have no idea how...
Ofcourse this is one just one preference and a super simple version of my code, but you'll get the idea. There are like 15 preference settings.
// EDIT //
I would also like to see how much the match rating is on screen!
Well, it was a good question from the start so I upvoted it and then wasted about 1 hour to produce the following :)
Data
I have used a DB named test and table named t for our experiment here.
Below you can find a screenshot showing this table's structure (3 int columns, 1 char(1) column) and complete data
As you can see, everything is rather simple - we have a 4 columns, with id serving as primary key, and a few records (rows).
What we want to achieve
We want to be able to select a limited set of rows from this table based upon some complex criteria, involving comparison of several column's values against needed parameters.
Solution
I've decided to create a function for this. SQL statement follows:
use test;
drop function if exists calcMatch;
delimiter //
create function calcMatch (recordId int, neededQty int, neededSex char(1)) returns int
begin
declare selectedQty int;
declare selectedSex char(1);
declare matchValue int;
set matchValue = 0;
select qty, sex into selectedQty, selectedSex from t where id = recordId;
if selectedQty = neededQty then
set matchValue = matchValue + 10;
end if;
if selectedSex = neededSex then
set matchValue = matchValue + 10;
end if;
return matchValue;
end//
delimiter ;
Minor explanation
Function calculates how well one particular record matches the specified set of parameters, returning an int value as a result. The bigger the value - the better the match.
Function accepts 3 parameters:
recordId - id of the record for which we need to calculate the result(match value)
neededQty - needed quantity. if the record's qty matches it, the result will be increased
neededSex - needed sex value, if the record's sex matches it, the result will be increased
Function selects via id specified record from the table, initializes the resulting match value with 0, then makes a comparison of each required columns against needed value. In case of successful comparison the return value is increased by 10.
Live test
So, hopefully this solves your problem. Feel free to use this for your own project, add needed parameters to function and compare them against needed columns in your table.
Cheers!
Use the limit and offset in query:
SELECT sex FROM ledenvoorkeuren WHERE userid = you limit 10 offset 0
This will give the 10 users data of top most.
You can set a limit in your query like this:
SELECT sex FROM ledenvoorkeuren WHERE userid <> yourid AND sex <> yourpreferredsex limit 0, 10
Where the '0' is the offset, and the '10' your limit
More info here
you may try this
SELECT sex FROM ledenvoorkeuren WHERE userid = you limit 0, 10 order by YOUR_PREFERENCE

mysql update multiple column one row single statement vs multiple statements

I am trying to update 3 column values in a row in mysql only if any of the 3 values is different.
Say I have a table of
x,y,z,id columns
I have currently,
Method A
update foo set x = 'x_value', y = 'y_value', z = 'z_value' where id = 'unique_id'
and ((x <> 'x_value') or (y <> 'y_value') or (z <> 'z_value'))
I don't know much about the theoretical benchmarking/architecture of mysql, and I was wondering if the statements
Method B
update foo set x ='x_value' where id = 'unique_id' and ((x <> 'x_value'));
update foo set y ='y_value' where id = 'unique_id' and ((y <> 'y_value'));
update foo set z ='z_value' where id = 'unique_id' and ((z <> 'z_value'));
is better or superior.
I realize that Method B will only do one write and 3 reads if only one column has changed, vs 3 writes and 3 reads for the Method A. I just don't know if it is more time intensive because method B requires looking up the index row 3 times.
Thanks in advance!
Based on what I've read in the comments, I agree with octern that you should simply run an update. It will use significantly less resources and based on your table engine, it will free up your table/ row lock for less time, making your table perform a lot better.
However, if you insist on doing a check before doing a write, do so through PHP. Simply do a select statement, compare the code in PHP and then update the appropriate table(s). For example:
$res = mysql_query("SELECT * FROM table1 WHERE PK_ID = '0'");
$arr = mysq_fetch_assoc($res);
$update = false;
if ($arr["field_1"] != $_POST["field_1"])
{
$update = true;
}
if ($arr["field_2"] != $_POST["field_2"])
{
$update = true;
}
if ($update)
{
mysql_query(sprintf("UPDATE table1 SET field_1 = '%s', field_2 = '%s'", $_POST["field_1"], $_POST["field_2"]));
}
if (
Method B will of course be more costly, because you do 3 different selects vs Method A's single select / update on condition.
Its pretty much a comparison of 1 statement to 3 statements. 1 will be faster as they are both update statements.

incrementing a table column's data by one || mySql

iam having a table with columns like
id || counter
if i do something (some event) i want the counter's value(at a particular id) to increase by one , currently iam doing this :
//get current value
current_value = select counter from myTable where id='someValue'
// increase value
current_value++
//update table with current value
update myTable set counter=current_value where id='someValue';
currently iam running 2 queries for this, please suggest me some way do it in one step.
Just run the math in the database:
update myTable set counter = counter + 1 where id = 'someValue';

Best update query for mysql table

I have a file hosting site where I provide a point for every unique download to user.
Sample of my table
These points can be redeemed by user. So for example if a user redeems 100 points than what is the best query to reduce points available from each row till 100 points are reduced.
Thank You.
You should create two tables for this:
Table files
- id
- name
- size
Table points
- id
- file_id
(- user)
- points
Insert a new file:
INSERT INTO files (name, size) VALUES ('kat92a.jpg', 105544); // New file with ID 1
Now you can give points to a file, negative or positive:
INSERT INTO points (file_id, points) VALUES (1, 100); //Positive points
INSERT INTO points (file_id, points) VALUES (1, -10); //Negative points
And you can select the total number of points:
SELECT
files.name,
files.size,
(SELECT sum(points) FROM points WHERE file_id = 1) AS points
FROM files
WHERE id = 1
Alright, then, here's the SQL-dumb way I would do it. Hopefully an SQL guru will come around with a better solution. Note: This is pure pseudocode; write your own code based on this--it's not going to work out of the box.
$total_to_deduct = 100;
// Each time, get the row with the highest points
$top_points_query = "SELECT id, points FROM my_table ORDER BY points DESC LIMIT 1;"
do {
$result = do_query($top_points_query);
if($result) {
// I'm assuming you don't want to deduct more points from a row than it has
$num_to_deduct = min($result['points'], $total_to_deduct);
// Now deduct the points from the row we got earlier
$update_query = "UPDATE my_table SET points = points - $num_to_deduct
WHERE id = $result['id']";
if(do_query($update_query)) {
$total_to_deduct -= $num_to_deduct;
}
}
} while($total_to_deduct > 0); // If we still have points to deduct, do it again
Seems like you just need a simple update Statement and allows you to update the row and if it's more than 100 not update it.
update table set points = if( (points+<VALUE>) <= 100,points+<VALUE>,points) where id = <FILE ID>
This will check to see if the points is higher than 100, if it is then the update statement will just return no results. If the value is less than 100, then it will update the table and give you back the amount of rows that were updated.
Just add a column in your user table with the amount of redeemed points. Is that a viable solution for you?
Here is a pure SQL solution, but I warn you that (a) this is untested and (b) it's just a concept.
DECLARE curs CURSOR FOR
SELECT
id,
points,
FROM
points
WHERE
points > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET remPoints = 0;
OPEN curs;
SET remPoints = 100; /* modify this value, probably in your app */
REPEAT
FETCH curs INTO cId, cPoints;
IF remPoints >= cPoints THEN
UPDATE points SET points = 0 WHERE id = cId;
ELSE
UPDATE points SET points = points - remPoints WHERE id = cId;
END IF;
SET remPoints = remPoints - cPoints;
UNTIL remPoints <= 0;
CLOSE curs;

Categories