I have a query that determine a users rank based on their actions
$db->row("SELECT COUNT(ID) as `user_rank` FROM userdetails WHERE (userdetails.totalaction /4) >= ('$user_detailed->totalaction' /4) AND active = 1 AND frozen = 0",array());
The above will return there rank and I want to make one query to update all users at once.
Basically I want the >= ('$user_detailed->totalaction' /4) to become pure sql no php in the query
so if i had 100 users it will work out each of there ranks and update another field called current_rank is it possible to do this all in one query if so can someone help me out.
You simply need to replace ('$user_detailed->totalaction' /4) with your desired value to test against. For example if you want it greater than equal 10, then it should be:
$db->row("SELECT COUNT(ID) as `user_rank` FROM userdetails WHERE (userdetails.totalaction /4) >= 10 AND active = 1 AND frozen = 0",array());
figured out how to do it had to do two queries not one like i wanted ;) thanks for help
Related
I'm making 4 individual queries to a MySQL DB, all of which are identical except the WHERE parameters. 2 of which are:
$totalInvites = mysqli_num_rows(mysqli_query($con, "SELECT code FROM invites"));
$usedInvites = mysqli_num_rows(mysqli_query($con, "SELECT code FROM invites WHERE used IS NOT NULL"));
Is there a way of doing the $totalInvites query and from the returned table, do the WHERE call without doing another query?
If that's confusing, this is an example of what I mean:
$query = mysqli_query($con, "SELECT code FROM invites");
$totalInvites = mysqli_num_rows($query);
$usedInvites = mysqli_num_rows($query /*WHERE used IS NOT NULL*/);
I know that's not proper syntax but that's what I was trying say.
If you just want counts then retrieving the entire database and throwing out the results is not really a good idea. Instead jus task for a count:
SELECT COUNT(*) AS count, used
GROUP BY used
This will give you up to two rows, one count for those that are used and one that isn't presuming used has only NULL or a single non-null value.
Use as little SQL as possible:
SELECT if(used is null, 0, 1) AS used, code
FROM invites
And parse result in PHP according to what you need
SELECT
(SELECT code FROM invites) total
(SELECT code FROM invites WHERE used IS NOT NULL) used
I have one specific problem, I need to make function. User pass sttring into this function. String is MYSQL query and now, I want to change this mysql query as this, if I get for example SELECT student.name as person FROM student WHERE student.grade = 1 LIMIT 10 Now i need to create somethink like this (SELECT student.name as person FROM student WHERE student.grade = 1 LIMIT 10) WHERE person = "John" LIMIT 5 (watch person in where). The Select between ( and ) can be anythink, but my conditions after ) can be only aggregate functions, limit and where conditions. Is somethink like this possible? I need to create it in php. I hope you understand me.
Thanks for help.
As I said in my initial comment it is a very very bad idea, however you can simply do this:
SELECT *
FROM ([user supplied query]) AS theQ
WHERE theQ.person = "John"
LIMIT 5;
Once you wrap the user supplied query as a subquery, you can pretty much just use it like a table. Of course, in this case if the user supplied query does not contain a person field, the query will error.
Please help me figure out how to put the following two statements into one query. Your help is much appreciated.
$sql1 = 'SELECT COUNT(id) as total_cat_votes FROM votes WHERE category_id="1"';
$sql2 = 'SELECT COUNT(nominee_id') as total_nom_votes FROM votes WHERE category_id="1" AND nominee_id="16"';
My Idea here is I have a table called votes and I want to get the number of total votes for a specific category under category_id as well as the different total votes of each nominee under nominee_id. I hope I am clear enough.
Thanks for the help!
Use SUM() for the second count with a condition, using sum with a condition will result in a boolean as 0 or 1,also using aggregate functions without grouping them will result in a single row
SELECT COUNT(id) as total_cat_votes,
SUM(nominee_id='16') as total_nom_votes
FROM votes
WHERE category_id='1'
Hi I have this situation:
$query = $db->query('SELECT COUNT(*),* FROM memberFileReports');
$data = $query->fetch(PDO::FETCH_ASSOC);
print_r($data);
its not working ? any idea why and how to Count and get all items at the same time, and what if I want to Count (*) and then next Select will have Limit 0,5?
Is this what you're looking for?
SELECT * , CONCAT('', (SELECT COUNT(*) FROM memberFileReports)) AS total
FROM memberFileReports LIMIT 0,5
EDIT 1: Note that the 'total' will be a string
EDIT 2: As I understand , you want to get the total rows in the table, but only select 5 of them (as example)
EDIT 3: caps... and misspell
Use this
SELECT * , COUNT(*) FROM memberFileReports Group by column_id
Instead of this
SELECT COUNT(*),* FROM memberFileReports
EDIT:
try this
$data = $query->fetchAll();
instead of
$data = $query->fetch(PDO::FETCH_ASSOC);
I know this post is a little late but i figured i would tell you, the reason why you only get one row back is because you are using an aggregate function "COUNT()". that function makes it so the result is returned in one row. the way to show stuff in more than one row is to use a "GROUP BY" in your select statement and it will group the data by the condition you tell it to.
example... suppose we had an example table with 3 users..
one has 10 interactions
one 3
and one 33:
SELECT
e.UserID,
COUNT(e.interactions) as num_interactions
FROM example e
GROUP BY e.UserID;
that would show the number of interactions a user has made in the dummy example table.
OUTPUT:
UserID num_interactions
1 3
2 10
3 33
without the GROUP BY the aggregate would make the output look like this:
UserID num_interactions
1 46
just for future reference :)
This may be very simple but I am trying to do a query that will return the average of the results but I also want it to count the number of rows it used to get the average. For example:
Row 1 = 5
Row 2 = 2
Row 3 = 9
Row 4 = 1
Average = 4.25
Count = 4
Is there a way to do this with one query apposed to two. When i used the avg function is always just returns one row so my count is 1 instead of 4. I need a query that will average but also tell me how many records it went through.
I am trying to avoid using two queries. Thank you for your help.
This is pretty basic and should have been discoverable via search.
SELECT COUNT(field) as `count`, AVG(field) as `average` FROM table_name
In the terms you have stated - if you aren't GROUPing or things like that - you'd just write
SELECT COUNT(col) AS cnt, AVG(col) AS avg FROM tbl;
and you ought to have no problems. You get one row, with the fields cnt and col containing what you need.
The problem you're having is probably due to the use of mysql_num_rows to get the count, which is not correct.
forgot to add: or to the fact that you did not supplied your whole problem.
You can use multiple aggregate functions in a single query:
SELECT COUNT(*), AVG(field) FROM mytable
SELECT COUNT(*), AVG(YourColumn)
FROM YourTable