Randomly update columns with mysql [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm building a browser game which you can shoot missiles on factories.
This is example of a similar SQL table:
user_ID iron steel metal gold
1 21 30 39 25
2 7 10 25 50
I need each missile to destroy 1 factory.
For example I shoot 10 missile, so 10 factories are destroyed. But I need it to have completely randomly and even.
The problem is if according to this SQL table, if I shoot for example 40 missile on user_ID '2'.
In best case I destroy 10 factories of each kind. But how can I do it when I don't know if there is enough of each kind?

First, divide the number of missiles by the number of factory types that have at least 1 factory. If all the types have at least this many factories, subtract this from each column.
If any of the types have less than this, they'll be totally wiped out. Reduce them to 0 and subtract their original totals from the number of missiles. Then start again, using just the remaining missiles.
Repeat this until you use up all the missiles or all factories are wiped out.

Related

mysql table design, not sure on best approach [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am building a mysql database to store golf scores.
I am not sure on the best approach to store the round information.
Each round is made up of either 9 or 18 holes and for each hole I need to store
Hole id
Number of shots
Stableford points
Green in regulation
Fairway hit
Number of putts
Number of penalty shots
My question is should I have one huge table, that stores all of this. Like a rounds table. and have the above 7 fields 18 times for each hole.
Or should I have a smaller rounds table that just contains the date played etc and then another table such as scores that just has the 7 fields, and have multiple rows in that table to make up the complete round?
I guess I am asking in terms of which would perform better and which is the better design?
Thanks
Definitely two tables. First, let's name it rounds will contain data relevant to round itself, such as date, id of the golf terrain etc. The other, let's name it hole, will have 7 aforementioned fields, together with round_id field that will reference round that particular hole belongs to.
Main benefits are clearer design and avoidance of redundant data. If you keep everything in one huge table, each row would need to contain not just fields relevant to the single hole, but also fields relevant to the whole round (date, id of the terrain..) -> same data in many rows, unnecessary. What if you mistakenly enter wrong date? You would have to change it in all 9 to 18 rows instead of only one.
See also:
Normalization in MySQL
Database normalization
Divide your information as much as possible. Otherwise you'll face alot of redundant data.

looping through 1 million records update with where VS update sql with where IN [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have table of more than 2 million records..
I may need to update ; say 1 million records;
which is more efficient between below two:
Looping through million records and run update query with where id=id million times ?
or
get all million ids and implode it ; and use it in single update query with WHERE id IN (id1,id2,.....id100000)
Can any experts help me with this?
Generally, the option 1 you are suggesting will be slower than the option 2, however the option 2 will eat a lot more memory and could potentially block your table for few minutes ...
it really depends on your DB architecture, and the context (is it on a Dev or Prod server? is it an operation you'll need to repeat ? etc.), but I would opt for a third option :
update by batch: For instance, you could update 10000 rows per 10000 (update the first 10000 rows, then the next 10000 rows, etc...)
10000 is obviously an example and, as suggested by Dagon in his comment, you should test the different options if you can.
Note : concerning your option 2, you should consider a better WHERE clause than ALL the IDs you want to update in an IN (you could for instance use the same conditions you'd use here : "get all million ids and implode it ;")

How do I get average rating and round up to nearest 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a voting table called product_reviews, with a field that is ENUM from 1 to 5 called rating.
I want to get average rating from all rows with product_id 1665. I am using a star rating system so the average cannot be with decimals. Must be a number from 1 to 5.
Thank you very much.
SELECT round(avg(rating)) as average_review,
count(rating) as number_of_reviews
FROM product_reviews
WHERE product_id = 1665
You need to use round here since avg will return a value between 1.0 and 5.0, and if you use floor or ceil you're effectively eliminating 1 or 5 from the possible results unless all reviews on the product have that score.
Also, you'll alienate your users if they can openly see a product got 50 reviews with 1 star, and 1 review with 2 stars, and you're showing an average score of 2 stars next to that. It'll make your site seem unreliable which is usually not a good thing for a site that contains reviews. For this reason most star rating systems are also capable of showing partial stars to make it more precise.

PHP Updating Array of Appointments Slots [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a list of appointments for a day. There are 10 available appointments 15 minutes apart (I'm referring to each 15 minute appointment as a slot) and the list might look like this:
1 Unavailable
2 Unavailable
3 Available
4 Available
5 Available
6 Available
7 Available
8 Available
9 Unavailable
10 Unavailable
There are certain types of appointments that require a minimum number of slots. For example if the appointment type is "long" it needs 3 consecutive slots. So in the above example the possible combinations could be
3 Available
4 Available
5 Available
or
4 Available
5 Available
6 Available
or
5 Available
6 Available
7 Available
or
6 Available
7 Available
8 Available
I then need to update the list to make 7 and 8 unavailable:
7 Unavailable
8 Unavailable
as the appointment cannot start at these times as there are not 3 consecutive available slots starting at these times.
I'm completely stumped as to the logic/rule I can use to implement here? I'm hoping there is something abstract that I can use and just feed in the number of slots required.
This is a simple textbook exercise of a function within a function.
Your first function needs to scan for the first available slot and then passes on the context to the second function that will look ahead from the current position and make sure the next 1/2/3/4/etc slots are available.
If available, the second function returns true, the first function just takes into account that it needs 1/2/3/4/5/etc slots so one possible choice is to use:
3/4/5
4/5/6
5/6/7
6/7/8
Note: You can use 1 single function with two imbricated loops and an $i, $k index. I prefer two functions for the sake of simplify and readability!

How do make this calculation to achieve the answer I want? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a bit of a puzzle. Look at the sum and result below:
$markTotal += ($session['Mark'] / 100 * $session['SessionWeight']);
Result on Browser:
Module: CHI2550 - Modern Database Applications 41.2 (this is $markTotal)
Session: AAB 72(this is $session['Mark']) 20% (this is $session['SessionWeight'])
Session: AAE 67(this is $session['Mark']) 40% (this is $session['SessionWeight'])
As you can see the answer to the calculation above is right, the answer is 41 .2 as it adds up the two session marks, divide by 100 and then times it by the total amount of the percentage.
But I want to include a total mark where except it is out of the total session percentage (60% for above example), it is out of a 100% but I can not work as simple as that as in above example one session is worth more than other. I have worked out that the answer for the total mark of the above example out of 100% should be 69, but how do I achieve this in my calculation.
Thank you and any help is much appreciated :)
You'd need to add up the total marks as well. So if session AAB has 90 total marks available (and the student got 72) and AAE has 80 marks (and got 67) then it'd be
(72) + (67) 14.4 26.8 41.2
(--) * 0.2 + (--) * 0.4 = ---- + ---- = ---- = 79.23%
(90) + (80) 18 34 52

Categories