I have a database which has many columns, but 3 of them I need to sum up. a, b & c .... I need to add a total column for every row (40+ rows) so each time a new score is entered into a,b or c ... the total column is updated for that particular row.
At the moment, I have the following Update that runs to enter a new score.
mysql_query("UPDATE national_reqs SET " . $phase . " = '" .$score . "' WHERE dog_name ='" . $dog_name . "'");
Then when I pull the data back out, I use the following query (and I want to sort by a new "Total" column)
$result = mysql_query("SELECT regfor,cat_num, ipolevel,handler,dog_name,a,b,c,score_time FROM 2015_national_reqs WHERE (id<=190) ORDER BY cat_num ASC") or die(mysql_error());
I think I need a trigger to make this work properly ? I can create a new column for "Total" of type int.
Thoughts ?
update [table] SET a = [value], d = (a+b+c) where id = [id];
Cheers
Related
I have 2 tables. One called local_requestors and one called local_aid. They both have different sets of data, but they both share a column, the column in local_requestors called suser_scheme is the same as the data in the local_aid table column called scheme_id.
I want to query my tables to get all data from the local_requestors table, where the ID matches that of the row in the local_aid table.
I've used the following...
$stmt = $conn->prepare('SELECT *
FROM local_requestors
INNER JOIN local_aid
WHERE local_requestors.user_scheme=local_aid.scheme_id
AND local_requestors.id = :aid_id');
$stmt->execute(['aid_id' => $user_id]);
while($row = $stmt->fetch()) {
echo '<p>' . $row['user_name'] . '</p>';
}
This is all on a local page that I can only access, but to query the table I post some $_GET[] variables...
$user_id = $_GET['id'];
$scheme_id = $_GET['scheme_id'];
No matter what I try, I keep getting the same row from the local_requestors table, repeated around 10 times.
My goal is to get print the name of the user in the record from local_aid and a number of related records from local_requstors.
echo 'Hello ' . $aid_name . ', there is ' . $rowCount . ' people that need help in your area.;
I think you may have ment
SELECT *
FROM local_requestors
INNER JOIN local_aid ON local_requestors.user_scheme=local_aid.scheme_id
WHERE local_requestors.id = :aid_id
And from your description of what you want you may want a LEFT JOIN instead of a INNER JOIN
I have a table named resources with the following columns:
ress1
ress2
prod1
prod2
I've tried the code indicated in the following links, but I'm still stuck...
How to find sum of multiple columns in a table in SQL Server 2005?
Auto update sum of rows & columns in a table
SQL How to Update SUM of column over group in same table
I want to run a script to UPDATE ress1 = ress1 + prod1 and the same with the ress2. So I want to update into a column the sum of the column itself and the corresponding column, doing that for every rows from the table.
Here is some code that seems easy for me, but not working...
$sql = "SELECT * FROM ro_map";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
$new_a = $row['a'] + $row['c'];
$new_b = $row['b'] + $row['d'];
"UPDATE ro_map SET a = '$new_a',b = '$new_b'";
}
Ah...really sorry, guys :D I use mysql (with wampp server and HeidiSQL). And running this code don t give me any error ( if($conn->$sql == TRUE) will give me TRUE...
SOLVED! Cool, thanks all, I've learned something :)
I think you're over-complicating it. If I understand you correctly, you can just do this in one transaction:
Update resources
Set ress1 = ress1 + prod1,
ress2 = ress2 + prod2
I have a database table that I'm needing to change one field based on another. There are two records for each id "option_id." Each have a sort_order.
So for example:
12345, 1
12345, 2
I need to be able to find any records that have the same ID and have one for sort_order 1, and one for sort_order 2. Then, I need to change the sort_order of 1 to 0, and the sort_order of 2 to 1. I'd like to use php to do this is possible.
Assuming you can handle the connection strings yourself, (I'm assuming PDO here). I had to do number swapping almost exactly like this very recently.
Then do the following:
$sql = "SELECT * FROM table_name WHERE sort_order = 2";
while($row = $results->fetch(PDO::FETCH_ASSOC)){
$sql_1 = "UPDATE table set sort_order = 0 WHERE sort_order = 1 AND option_type_id = " . $row['option_id']; //then execute query
$sql_2 = "UPDATE table set sort_order = 1 WHERE sort_order = 2 AND option_type_id = " . $row['option_id']; //then execute query.
}
The logic is that you find the matching record, change the 1 to 0, then change the 2 to 1. Doing the opposite would really mess up the records.
How can I make a limit of showing the results? I need to limit it for 100 views.
In DB I have:
ID|NAME|PAGE|COUNT|DATE
In count I want to count untill 100 and then stop showing that ID. I could do it with count < 100. And then update the specific ID. I could get records with less than 100 views, but I couldn't manage to update count on the specific ID.
Row is showed with:
php code:
foreach($bannerGroups[0] as $ban) {
echo '<li class="right1">'.$ban->html().'</li>';
}
But I just don't know where to put the update in there. I tried, but all I got was to update only one ID. But it shows 4 on one page and randomizes them on refresh. So I don't know what to do.
Also I would like to say I am only learning php. Sorry for all the mess.
Code at http://pastebin.com/A9hJTPLE
If I understand correctly, you want to show all banners that have been previously-displayed less than 100 times?
If that's right, you can just add that to your WHERE clause:
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' WHERE `COUNT` < 100");
To update them all, you can either run a query while displaying each individual banner, or "record" the id of each and run a single query at the end, like:
$ids = array();
foreach($bannerGroups[0] as $ban) {
$ids[] = $ban['ID']; // record the ID; don't know how Banner
// class works, assuming uses indexes; maybe ID() method?
echo '<li class="right1">'.$ban->html().'</li>';
}
...
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
UPDATE:
Based off of a comment, your Banner class doesn't have a method to retrieve the individual banner's ID. In this case, you can record the ID values when you're building your banners array:
$ids = array();
while($row=mysql_fetch_assoc($bannerResult)) {
$banners[] = new Banner($row);
$ids[] = $row['ID']; // record the ID
}
// update the `count` on each record:
mysql_query('UPDATE table SET `COUNT` = `COUNT` + 1 WHERE ID IN (' . join(',', $ids) . ')');
sorry, but I got your question wrong...
first you have to insert a new sql-column like "viewcount" to the db...
on every read, you have to increment the value in viewcount...
for that behaviour (because, mysql does not allow sub-selects on update-clause on the same table), you have to fetch the results from db, as you do that, and pass all the primary-keys of the records to an array...
after the view-logic you have to fire up a query like:
UPDATE foo SET viewcount = viewcount + 1 WHERE id IN (1,2,3,4,5,6...,100);
where the IN-clause can be easily generated using your primary-keys-array with "implode(',', $arr);"
hope this helps.
$bannerResult = mysql_query("SELECT * FROM table WHERE page='cat' AND `count`<100");
#newfurniturey figured it out. in each foreach($banneruGroups added: $ids = $ban->getValue('id'); and then mysql_query("UPDATE dataa SET COUNT = COUNT + 1 WHERE id = '$ids'"); but is there any way to update them by adding query only once? And if the id is showed already 100 times i get Warning: Invalid argument supplied for foreach() in. Any idea how to fix it? I have 4 ids in DB . If one of them already have 100 views (count) then i get error!
Try to limit your data source for 100 items.
It's like OFFSET x LIMIT 100 in MySQL/PostgreSQL query or TOP 100 in MSSQL.
.Hi guys, i need a little help with a project that i'm currently working on.
I have this database where i have a table containing records of students from different departments namely:
Dept1
Dept2
Dept3
Dept4
Dept5
in my batch.php i have two dropdown menus for Batch(which is the name of the tables) and Department(a field used for sorting).
what i want to do is that when the user chooses a Batch and a Department and then clicks the submit button, it will automatically assign the values chosen to a MySQL query for the SELECT method. the SELECT query should now retrieve all records from the table sort it by department but will display first the Department that was chosen above.
.Any help would be much appreciated. Thanks in advance and more power!
You can sort by a boolean expression, which will return 1 when true and 0 when false.
<?php
$batch = mysql_real_escape_string($_POST['batch']);
$department = mysql_real_escape_string($_POST['department']);
$query = "SELECT * FROM `" . $batch . "`" .
" WHERE department = '" . $department . "'" .
" ORDER BY department = '" . $department . "' DESC, department;
?>
Update: You should validate the $batch argument that it is a valid table expected, the code as is is vulnerable to SQL injection.