• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do this query?

Hi guys,

for a project I'm working on now, I have to do a database query, and I'm having difficulties to figure out how to get the correct results. Maybe someone here could shed his light on the case Smile

Ok. I have a very simple database setup:


It's a script where people can virtually donate to a good cause. For each 'number', the organization donates 2 Euro's to one out of three causes (donation = 1, 2, or 3).

So the simplest way of doing this for each cause is:

"SELECT SUM(number * 2) AS cause1 FROM table WHERE donation = 1";

However, there an extra condition, that makes it a bit harder:

The maximum amount that one user (username) can donate is 10 Euro's. But it can be donated in different rows and to different causes.

How would you guys go about doing this?

Any advice is very much appreciated!


The most user friendly option would be that the cause select/radio button checks if the user still can donate or not. So in an ajax controller you can put this code
echo ($this->db->query('select count(*) as rows from table where donation = ?',array($input))->row()->rows < 5)?'they have enough money already':'You can give a little more';

Method chaining works with PHP5 only.

it's an ugly snippet, i just wanted to show something that worked but nobody should use it for production code.

Hello, how can I write query with Active Recird?

I need SELECT * FROM table WHERE id = MAX(id).


I'd try doing it like this:

$res = $this->db->get_where('table', 'id = MAX(id)');

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.