How can I summarize total amount of users earning from a column "price" values?
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= ".'$_SESSION['user']';
$statement = mysql_query($statement);
$user_earning= mysql_fetch_row($statement);
echo $user_earning;
you should add GROUP BY clause at select statment
$statement = "SELECT user_id, SUM(price)
FROM tbl_post
WHERE user_id= ".'$_SESSION['user']". "
GROUP BY user_id";
write above query instead of this query
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= ".'$_SESSION['user']';
i dont know about php but by see your query you want to sum of all price of a perticular user.
Try this, you have error in your query :
$statement = "SELECT user_id, SUM(price) FROM tbl_post WHERE user_id= '".$_SESSION['user']."' ";
$statement = mysql_query($statement);
$user_earning= mysql_fetch_row($statement);
echo $user_earning[1];// echo $user_earning['price'];
Your SQL query should look like that:
select user_id,sum(price) from tbl_post where user_id=? group by user_id;
Related
I'm trying to count number of messages each user got. I have a users with user's detail id, name etc. I have created another table users_msgs where I have id, msg_id, user_id. I want to display count of messages each user got. what will be the best way to do it?
The web application have more than 2000 users. so the script have to select all of them and count their messages. I think this is not the best solution.
I thinking of count rows for 1 user from users_msgs table as count and then querying the users table for user's name with his id from users_msgs table.
I have tried selecting all users without any limit:
SELECT * FROM users
then iterating over the results like so:
<?php
while ($user = mysqli_fetch_assoc($users)) {
$count = count_user_msgs($user['id']);
echo "{$user['name']} messages: $count";
}
?>
The count_user_msgs function looks like this:
<?php
$sql = "SELECT COUNT(id) as msgs_count FROM users_msgs WHERE user_id = ?";
$stmt = mysqli_stmt_init($db);
if (mysqli_stmt_prepare($stmt, $sql)) {
mysqli_stmt_bind_param($stmt, 's', $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$count = mysqli_fetch_assoc($result)['msgs_count'];
return $count;
}
return false;
?>
You need to group by each user and get the count:
$sql = "SELECT user_id, name, count(user_id) as msgs_count from table group by (user_id, name)";
$result = $mysqli->query($query)
while ($user = mysqli_fetch_assoc($users)) {
$user_id = $users['user_id'];
$msgs_count= $users['msgs_count'];
$count[$user_id] = $msgs_count;
echo "{$user['name']} messages: $msgs_count";
}
FWIW, You can get it in a single query...
SELECT u.name, count(m.user_id) message_count
FROM users u
LEFT JOIN users_messages m ON u.user_id=m.user_id
GROUP BY m.user_id, u.name
You want to count the number of msg_ids per user_id, so you'll want to GROUP BY user_id and count msg_id, like this:
$sql = "SELECT COUNT(msg_id) as msgs_count FROM user_msgs WHERE user_id = ? GROUP BY user_id";
The GROUP BY might not be necessary.
I want to count all the entries against id
i have used this query
select count(*) from table1 group by `id`
the problem is my table has 3 records
id A has 2 records
id B has 1 record
when i run this query using php it gives me 2 enteries
$mysqli = new mysqli("localhost","root", "", "db");
$query = $mysqli->prepare("SELECT COUNT(*) FROM `tble1` GROUP by `ID`");
$query->execute();
$query->store_result();
$rowsaff = $query->num_rows;
echo $rowsaff;
EDIT: misunderstand the question.
You have to SELECT the count of the distinct id:
$query = $mysqli->prepare("SELECT COUNT(distinct `ID`) FROM `tble1`");
How can I implement something like this in mysql?
$query1 = "SELECT id FROM table WHERE username = 'John'";
$query2 = "SELECT id FROM table WHERE username= 'Parsa'";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
$result = mysql_query($query) or die('Query faild'.mysql_error());
$myrecord = mysql_fetch_assoc($result);
Try this
$query1 ="SELECT GROUP_CONCAT(id) FROM table WHERE firstname in('John','Parsa')";
$query = "SELECT * FROM table WHERE id IN ($query1)";
you have two identical queries , you could just have one . and use IN , not BETWEEN.
You can put those 3 queries in to one query:
$query = "SELECT * FROM table WHERE id
BETWEEN
( SELECT id FROM table WHERE firstname = 'John' GROUP BY id )
AND
( SELECT id FROM table WHERE firstname = 'Parsa' GROUP BY id )
";
although your query doesn't mean anything; you need "()" for subqueries to work.
$query1 = "(SELECT id FROM table WHERE username = 'John')";
$query2 = "(SELECT id FROM table WHERE username= 'Parsa')";
$query = "SELECT * FROM table WHERE id BETWEEN $query1 AND $query2";
u can use a subselection:
SELECT * FROM table WHERE id BETWEEN ($query1) AND ($query2)
But be careful: The Subselection result must be an Integer.
I was thinking of accomplishing the following as a PHP multi_query. But I'm trying to figure out how to pass the column value from the select query to the insert and update queries.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$query .= "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query .= "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
Problem is, I'm not sure how to pass the link_id value to the other queries. So I'm thinking I might have to rearrange the queries into one, but again, I'm just not sure how to pull that off.
Anyone got any suggestions?
You need to execute select query 1st then use its output to execute 2nd & 3rd query.
$query = "SELECT tbl_links.link, link_id
FROM tbl_links
INNER JOIN tbl_items ON tbl_links.item_id = tbl_items.item_id
WHERE tbl_items.item_name like '".$items_name[$counter]."'
AND NOT EXISTS (
select link_id
from tbl_clickedlinks
where tbl_clickedlinks.link_id = tbl_links.link_id
AND tbl_clickedlinks.cust_id = '$items_custID[$counter]'
)
limit 0, 1;" ;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$query2 = "INSERT INTO tbl_claimedlinks (cust_id, link_id, claim_time) VALUES ('$items_custID', $row['link_id'], NOW()) ;";
$query3 = "UPDATE tbl_links SET click_count = click_count+1 where link_id = '$linkID' ;";*/
mysql_query($query2);
mysql_query($query3);
}
Please see my query below. Right now its look for system_id from contacts where system_id = '$sid' but i want the query to ALSO look for friend_id from contacts where system_id = '$sid'
$tre = mysql_query("SELECT System_id, Full_name FROM accounts
WHERE Full_name LIKE '". mysql_real_escape_string($_GET['q'])."%' LIMIT 5");
while($re=mysql_fetch_array($tre))
{
$qry=mysql_query("SELECT System_id FROM contacts WHERE
System_id='". $sid ."' AND Friend_id='". $re['System_id'] ."'");
if(mysql_num_rows($qry)>0){
if($sid!=$re['user_id']){
$obx['id']=$re['System_id'];
$obx['name']=$re['Full_name'];
$arr[]=$obx;
}
}
How do i tweak it?
Thanks!
Update:
Upon looking up some of the comments below i structured this query that works as expected:
SELECT DISTINCT contacts.friend_id, accounts.full_name,
accounts.system_id
FROM contacts, accounts
WHERE (contacts.system_id = '$sid' AND contacts.friend_id
= accounts.system_id) OR (contacts.friend_id = '$sid'
AND contacts.system_id = accounts.system_id)
The only problem is how do i change the SELECT query from this:
$tre = mysql_query("SELECT System_id, Full_name FROM accounts
WHERE Full_name LIKE '". mysql_real_escape_string($_GET['q'])."%' LIMIT 5");
To $tre:
SELECT DISTINCT contacts.friend_id, accounts.full_name,
accounts.system_id
FROM contacts, accounts
WHERE (contacts.system_id = '$sid' AND contacts.friend_id
= accounts.system_id) OR (contacts.friend_id = '$sid'
AND contacts.system_id = accounts.system_id)