Above is a scheme i drew. There's an ideal case, when content.user are groupped. But usually they are not groupped.
What i meant in this scheme is:
At the first step, i select users.monetos WHERE users.id = content.user
At the second step, i decrement users.monetos with every content.cpc value (2.1 , 2.2)
When simulating this:
select content.user (9)
select users.monetos Where users.id=content.users (15)
So we have 15 value for users.monetos for users.id=9, now we go back to content table
and:
decrement 15 value with 8 (content.cpc) (15-8=7 > 0 -> go to step 2)
decrement 7 (result from previous step) with 10 (content.cpc) (7-10=-3 <0 -> update
content set active='0' where content.id= (current id when a negative result obtained) )
And like this for every content.user
More extended - I want to select contet.* rows which have content.active = 1 (n). Having this data, SELECT users.monetos WHERE users.id=content.user from previous query.
And now, by maximum (n) steps i decrement users.monetos value by content.cpc value
and on the moment when **users.monetos=0 or less than 0, i want to update content and SET active='0'**
By words, i want like to share users.monetos amount to each content entry (content.cpc for each). And there's no more users.monetos make the current content
entry inactive. And do this vor every content.user
What i'he done at this moment is shown below. I now it looks really bad, but i already don't know what to do. Count on you guys. Thank you.
$query = "select content.id, content.cpc, conent.user, content.active from content a
join users b on a.user=b.id
group by b.id where a.active='1'";
/** cycle each user **/
foreach($rows = $connector->fetchArray($query) as $row ) {
$monetos = $row['monetos'];
$query = "select id, cpc from content where user={$row['id']}";
/** cycle each users content **/
foreach($contents = $connector->fetchArray($query) as $content) {
echo $monetos;
$monetos -= $content['cpc'];
if($monetos <= 0) {
$disable[] = $content['id'];
}
}
if( isset($disable) ) {
$connector->query("update content set active='0' where id in(".implode(',',$disable).")");
}
}
By using GROUP_CONCAT we group the IDs and CPCs separated by a comma for later use and with GROUP BY the user ID we will get a single row of result per user.
On the foreach we deduce each CPC from the MONETOS and from there we set who needs to be disabled to the $to_disable array that is later used to disable all the id's needed to.
$query = "SELECT b.id AS user_id,
b.monetos,
GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids,
GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc,
FROM content a
JOIN users b
ON a.user = b.id
GROUP BY b.id";
$to_disable = array();
$to_enable = array();
foreach($rows = $connector->fetchArray($query) as $row)
{
$monetos = $row['monetos'];
$data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
echo "USER {$row['user_id']} currently have {$monetos}!<br>\n";
foreach ($data as $content_id => $cpc)
{
$monetos -= $cpc;
echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>\n";
if ($monetos <= 0)
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>\n";
$to_disable[] = $content_id;
}
else
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>\n";
$to_enable[] = $content_id;
}
}
echo "<br>\n";
}
if (sizeof($to_disable) > 0)
{
$connector->query("UPDATE content
SET active = 0
WHERE id IN (".implode(',',$to_disable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_disable).")<br>\n";
if (sizeof($to_enable) > 0)
{
$connector->query("UPDATE content
SET active = 1
WHERE id IN (".implode(',',$to_enable).")");
}
echo "UPDATE content SET active = 0 WHERE id IN (".implode(',',$to_enable).")";
Using your SQL dump this is what I get:
USER 9 currently have 15!
USER 9 after CONTENT 16 now have 10!
USER 9 after CONTENT 30 now have 5!
USER 9 after CONTENT 17 now have 4!
USER 9 after CONTENT 31 now have -1!
USER 9 should have the CONTENT 31 disabled!
USER 9 after CONTENT 18 now have -4!
USER 9 should have the CONTENT 18 disabled!
USER 9 after CONTENT 32 now have -9!
USER 9 should have the CONTENT 32 disabled!
USER 9 after CONTENT 20 now have -13!
USER 9 should have the CONTENT 20 disabled!
USER 9 after CONTENT 33 now have -18!
USER 9 should have the CONTENT 33 disabled!
USER 9 after CONTENT 21 now have -22!
USER 9 should have the CONTENT 21 disabled!
USER 9 after CONTENT 34 now have -26!
USER 9 should have the CONTENT 34 disabled!
USER 9 after CONTENT 22 now have -31!
USER 9 should have the CONTENT 22 disabled!
USER 9 after CONTENT 24 now have -36!
USER 9 should have the CONTENT 24 disabled!
USER 9 after CONTENT 26 now have -41!
USER 9 should have the CONTENT 26 disabled!
USER 9 after CONTENT 29 now have -45!
USER 9 should have the CONTENT 29 disabled!
USER 10 after CONTENT 28 now have 95!
USER 11 after CONTENT 27 now have -4!
USER 11 should have the CONTENT 27 disabled!
And the UPDATE result:
UPDATE content SET active = 0 WHERE id IN (31,18,32,20,33,21,34,22,24,26,29,27)
And here is the sample code used to read the data as is:
<?php
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
$con = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT b.id AS user_id,
b.monetos,
GROUP_CONCAT(a.id ORDER BY a.id DESC) AS content_ids,
GROUP_CONCAT(a.cpc ORDER BY a.id DESC) AS cpc
FROM content a
JOIN users b
ON a.user = b.id
GROUP BY b.id";
$result = $con->prepare($sql);
$result->execute();
if ($result->rowCount() == 0)
{
die('No data found...');
}
$to_disable = array();
$to_enable = array();
foreach($result->fetchALL(PDO::FETCH_ASSOC) as $row)
{
$monetos = $row['monetos'];
$data = array_combine(explode(',',$row['content_ids']), explode(',',$row['cpc']));
echo "USER {$row['user_id']} currently have {$monetos}!<br>\n";
foreach ($data as $content_id => $cpc)
{
$monetos -= $cpc;
echo "USER {$row['user_id']} after CONTENT {$content_id} now have {$monetos}!<br>\n";
if ($monetos <= 0)
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} disabled!<br>\n";
$to_disable[] = $content_id;
}
else
{
echo "USER {$row['user_id']} should have the CONTENT {$content_id} enabled!<br>\n";
$to_enable[] = $content_id;
}
}
echo "<br>\n";
}
if (sizeof($to_disable) > 0)
{
$ids = implode(',',$to_disable);
$sql = "UPDATE content
SET active = 0
WHERE id IN ({$ids})";
$disable = $con->prepare($sql);
$disable->execute();
echo "UPDATE content SET active = 0 WHERE id IN ({$ids})<br>\n";
}
else
{
echo "Nothing was disabled...<br>\n";
}
if (sizeof($to_enable) > 0)
{
$ids = implode(',',$to_enable);
$sql = "UPDATE content
SET active = 1
WHERE id IN ({$ids})";
$enable = $con->prepare($sql);
$enable->execute();
echo "UPDATE content SET active = 1 WHERE id IN ({$ids})";
}
else
{
echo "Nothing was enabled...";
}
$con = NULL;
Can you try two UPDATE statements?
UPDATE
users u JOIN
( SELECT user, SUM(cpc) AS cpc FROM content GROUP BY user) as c ON (u.id=c.user)
SET u.monetos = u.monetos - c.cpc;
UPDATE content AS c
SET c.active = 0
WHERE
(SELECT u.monetos FROM users u WHERE c.user = u.id) <= 0;
I didn't get if users.monetos was allowed to go negative or not. If not you can add an IF() check to the SET u.monetos
SQL Fiddle
Related
I am creating message conversation script between two users in PHP MYSQLI.I want to know how to count unread message between two users.My script not showing anything.
my database pm table
id from_id to_id msg sent_date read
1 2 3 hi how are you? 2019-12-05 04:14:20 1
2 3 2 fine 2019-12-05 05:15:58 0
3 2 3 hi 2019-12-05 03:20:34 1
4 5 2 hi 2019-12-05 08:30:40 0
Here is my source code
<?php
$unread_messages = 0;
if (isset($_SESSION['userid'])) {
$session_id = $_SESSION['userid'];
}
$sql = ("SELECT COUNT(*) AS unread_messages
FROM pm
WHERE pm.to_id = ? and pm.from_id=from_id
AND read = 0");
if ($stmt = $con->prepare($sql)) {
$stmt->bind_param('i', $session_id);
$stmt->execute();
}
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $unread_messages;
}
}
?>
I am creating message conversation script between two users.
I would phrase this as:
select sum(*) cnt_unread
from pm
where
read = 0
and (
(pm.from_id = :userid1 and pm.to_id = :userid2)
or (pm.from_id = :userid2 and pm.to_id = :userid1)
)
This gives you the count of unread messages between :userid1 and :userid2 (whether from user 1 to user 2 or the other way around).
SET #u1 = 2; -- id of first user
SET #u2 = 3; -- id of 2nd user
SELECT count(`read`) as unread FROM pm
WHERE `read` = 0 AND (from_id in (#u1,#u2) OR to_id in (#u1,#u2));
I have two queries, one which will get a list of IDs from the main table, and another which will get all records in another table that relate to that ID.
I'd like to split it into pages of results, but my google searches are only coming up with people who want to have a certain number of results per column value, not who want to limit the overall count by it.
The main table has a column that contains the number of records, so actually reading the other table isn't needed. The limit would be used as a minimum value, so if there are still records left in the current group after the limit, it'll continue displaying them. That'd be somehow calculated as part of the offset so it can start in the correct place.
Here's an example of what I mean:
Table 1:
ID | Records
1 | 2
2 | 3
3 | 28
4 | 7
...
Table 2 (contents don't need to be known for this question):
ID | GroupID | Value
1 | 1 | x
2 | 1 | x
3 | 2 | x
4 | 2 | x
5 | 2 | x
6 | 3 | x
...
If the limit was given as 3 for example, both 1 and 2 should display on the first page, since just 1 by itself is under the limit. The next page will then start on 3, and that'll take up the entire page.
I could just manually count up using PHP until I reach the limit, though it might end up going slow if there were a lot of pages (I've no idea if mysql would be any better in that regard though). Here's a quick example of how that'd work to get the offset:
$page = 2;
$limit = 40;
$count = 0;
$current_page = 1;
$query = 'SELECT ID, Records FROM table1';
$stmt = $conn->prepare($query);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$count += $row['Records'];
if($count > $limit){
$current_page ++;
$count = 0;
if($current_page == $page){
$start_id = $row['ID'];
break;
}
}
}
Updated:
A "custom" Pagination is what you're looking for. So if you're planning to hop from page to page, you can't use hardcoded $page and $current_page values. Those values should be generated dynamically once you're on a particular page. In fact, you should have the ID column value in the query part of the URL so that the pagination links could satisfy your business logic.
Assuming the fact that your ID column value starts from 1, your code should be like this:
$id = isset($_GET['id']) && is_numeric($_GET['id']) ? $_GET['id'] : 1;
$limit = 40;
// Display query results based on particular ID number
$query = 'SELECT ID, Records FROM table1 WHERE ID >= ' . $id;
$stmt = $conn->prepare($query);
$stmt->execute();
$rowCount = $stmt->rowCount();
if($rowCount){
$total = 0;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
if($total <= $limit){
$total += $row['Records'];
// Display $row details here
}else{
// Get the next $id value
$id = $row['ID'];
break;
}
}
}
// Display relevant ID links
$query = 'SELECT * FROM table1';
$stmt = $conn->prepare($query);
$stmt->execute();
$idArr = array();
$total = 0;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
if($total == 0){
$idArr[] = $row['ID'];
}
if($total <= $limit){
$total += $row['Records'];
}else{
$total = 0;
}
}
foreach($idArr as $idValue){
if($idValue == $id){
echo '<a>'.$idValue.'</a> ';
}else{
echo ''.$idValue.' ';
}
}
Sidenote: If the ID column value of your table doesn't start from 1, then use a separate query to get the first ID value and update the following statement,
$id = isset($_GET['id']) && is_numeric($_GET['id']) ? $_GET['id'] : <FIRST_ID_VALUE>;
If you only need to create the "Next" button, you could try this way:
select t1.ID, t1.Records
from table1 t1
left join table1 t2
on t2.ID < t1.ID
and t2.ID > :last_selected_id_1
where t1.ID > :last_selected_id_2
group by t1.ID, t1.Records
having coalesce(sum(t2.Records), 0) < :limit
order by t1.ID
:last_selected_id_x is the last ID from the current page or 0 for the first page.
http://rextester.com/FJRUV28068
You can use MySQL session variables to create page links:
select page, min(ID) as min_id, max(ID) as max_id
from (
select ID
, #page := case when #sum = 0 then #page + 1 else #page end as page
, #sum := case when (#sum + Records) >= :limit
then 0
else #sum + Records
end as sum
from table1
cross join (select #sum := 0, #page := 0) initvars
order by ID
) sub
group by page
The result would look like:
page | min_id | max_id
1 | 1 | 2
2 | 3 | 3
3 | 4 | 4
http://rextester.com/LQVJWP18655
Note that it is officially (as of documentation) not recomended to use the session variables like that (read and write in one statement). Feature versions may break your code.
Here's the result I ended up with, I ended up basing it off Rajdeeps answer but tweaked so that it'll allow sorting.
$query = 'SELECT ID, Records FROM table1 ORDER BY something';
$stmt = $conn->prepare($query);
$stmt->execute();
$id_array = array(); // For storing each ID
$current_ids = array(); // For storing the current ID pages
$initial_ids = array(); // For storing the first page, in case the page is too high
$count = 0;
$current_page = 1;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$already_reset = false;
// Move the session to the next page if it's more over the limit than under
// For example, if a limit of 10 has 6 records already, and the next ID is 9 records, then start a new page
if($id_array and $count + $row['Records'] / 2 > $max_links){
array_push($id_array, $last_id);
$current_page ++;
$count = 0;
$already_reset = true; // Set a marker so that if this ID is larger than the limit then don't start a 2nd new page
}
// Backup the first results in case page is too high and needs to be reset to 1
if($current_page == 1){
array_push($initial_ids, $row['ID']);
}
$count += $row['Records'];
// The values that appear here are the results for the selected page
// They may not be in sequence so store the individual IDs
if($_GET['page'] == $current_page){
array_push($current_ids, $row['ID']);
}
// Start a new page if over the limit
if($count > $max_links and !$already_reset){
$current_page ++;
$count = 0;
array_push($id_array, $row['ID']);
}
$last_id = $row['ID'];
}
array_push($id_array, $last_id);
$total_pages = count($id_array);
// If page is invalid revert to default
if(!$current_ids){
$current_ids = $initial_ids;
$_GET['page'] = 1;
}
$_GET['page'] = max(1, min($total_pages, intval($_GET['page'])));
$current_ids is an array of the IDs for the current page, and $total_pages is pretty self explanatory.
Short about my tables: I have 2 tables: content, users
content structure is something like:
id | title | cpc | user | active
users is:
id | user | monetos
Idea - I want to select contet.* rows which have content.active = 1 (n). Having this data, SELECT users.monetos WHERE users.id=content.user from previous query.
And now, by maximum (n) steps i decrement users.monetos value by content.cpc value
and on the moment when *users.monetos=0 or less than 0, i want to update content and SET active='0'*
By words, i want like to share users.monetos amount to each content entry (content.cpc for each). And there's no more users.monetos make the current content
entry inactive. And do this vor every content.user
What i'he done at this moment is shown below. I now it looks really bad, but i already don't know what to do. Count on you guys. Thank you.
$kak2 = array();
$rs16 = $connector->query("SELECT user FROM content WHERE active='1'");
while($rw16 = $connector->fetchArray($rs16))
{
$users_ids[] = $rw16['user'];
}
$user_info2 = $connector->fetchArray("SELECT monetos,id FROM users WHERE id IN (".implode(',',$users_ids).")");
while($user_info = $connector->fetchArray($user_info2))
{
$current_entry_info2 = $connector->query("SELECT cpc,id FROM content WHERE user='$user_info[id]' ORDER BY date DESC");
while ($current_entry_info = $connector->fetchArray($current_entry_info2))
{
$user_info['monetos']= $user_info['monetos'] - $current_entry_info['cpc'];
if($user_info['monetos'] = 0)
{
$updt = $connector->query("UPDATE content SET active='0' WHERE id='$current_entry_info[id]' LIMIT 1");
}
}
}
I think i have got the idea of what you are trying to do so have contructed a small code snippit which i think should do what you are after.
note this is untested code
/** get the cost of all content for all users **/
$query = "select b.id, sum(a.cpc) as cpc from content a
join users b on a.user= b.id
group by b.id where a.active = 1"
/** cycle each user **/
foreach($rows = $connector->fetchArray($query) as $row ) {
$menotos = $row['cpc'];
$query = "select id, cpc from content where user={$row['id']}"
/** cycle each users content **/
foreach($contents = $connector->fetchArray($query) as $content) {
$menotos -= $content['cpc'];
if($menotos <= 0) {
$disable[] = $content['id'];
}
}
if( isset($disable) ) {
$connector->query("update content set active=0 where id in(" . implode(',', $disable) . ")";
}
}
Above is a scheme I drew. There's an ideal case, when content.user are grouped. But usually they are not grouped.
What i meant in this scheme is:
At the first step, I select users.monetos WHERE users.id = content.user
At the second step, I decrement users.monetos with every content.cpc value (2.1 , 2.2)
When simulating this:
select content.user (9)
select users.monetos Where users.id=content.users (15)
So we have 15 value for users.monetos for users.id=9, now we go back to content table
and:
decrement 15 value with 8 (content.cpc) (15-8=7 > 0 -> go to step 2)
decrement 7 (result from previous step) with 10 (content.cpc) (7-10=-3 <0 -> update
content set active='0' where content.id= (current id when a negative result obtained) )
And like this for every content.user
More extended - I want to select contet.* rows which have content.active = 1 (n). Having this data, SELECT users.monetos WHERE users.id=content.user from previous query.
And now, by maximum (n) steps I decrement users.monetos value by content.cpc value
and on the moment when **users.monetos=0 or less than 0, i want to update content and SET active='0'**
By words, I want like to share users.monetos amount to each content entry (content.cpc for each). And there's no more users.monetos make the current content
entry inactive. And do this vor every content.user
What i'he done at this moment is shown below. I now it looks really bad, but I already don't know what to do.
$query = "select content.id, content.cpc, conent.user, content.active from content a
join users b on a.user=b.id
group by b.id where a.active='1'";
/** cycle each user **/
foreach($rows = $connector->fetchArray($query) as $row ) {
$monetos = $row['monetos'];
$query = "select id, cpc from content where user={$row['id']}";
/** cycle each users content **/
foreach($contents = $connector->fetchArray($query) as $content) {
echo $monetos;
$monetos -= $content['cpc'];
if($monetos <= 0) {
$disable[] = $content['id'];
}
}
if( isset($disable) ) {
$connector->query("update content set active='0' where id in(".implode(',',$disable).")");
}
}
You can try this code:
$query = "select * from users";
foreach($rows = $connector->fetchArray($query) as $row ) {
$a='';
$query = "select * from content where user='{$row[id]}' and active='1' ";
foreach($contents = $connector->fetchArray($query) as $content ) {
$a++;
$a_min=$a-1;
$test[$a]=$row[monetos]-$content[cpc];
if($a==2){
if($test[$a_min] > 0){
$test2=$test[$a_min]-$content[cpc];
if($test2 < 0){
$connector->query("update content set active='0' where id='$content[id]'");
}
}
}
}
}
I have 4 Tables (listed bellow) and need:
get last 10 Chats from Room 3 without banned users
show nickname for fromuserid
HIDE Users $userid dont like to see table "HIDE"
Table 1 "chats"
ID(autoinc) fromuserid roomid text
1 23 3 bla
2 14 1 bla
3 11 3 bal
Table 2 "user" /shorted/
ID(autoinc) nickname banned
1 chris 0
2 paul 1 // 1 = banned
Table 3 "hide"
ID(autoinc) orguser hideuser
1 12 3
2 33 12
Right now i solved it with PHP Routine, but I have to go through EACH result and make always a new query, that needs too long;
$userid = 1; // actual user
// List all chats and show userid as nickname
$sql_com = "SELECT user.id, user.nickname, chats.text, chats.id ".
" FROM chats, user".
" WHERE ".
" chats.fromuserid = user.id ".
" AND chats.roomid = 3 ".
" AND user.banned != 1 ".
" ORDER BY chats.id DESC";
$result = mysql_query ($sql_com);
$count = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
$dontshow = false;
// Filter : dont show users $userid dont like to see (table "hide")
$sql_com2 = "SELECT id from hide WHERE ( (orguser = ".$userid.") AND (hideuser = ".$row[0].") ) ";
if ($result2 = mysql_query ($sql_com2))
{
if (mysql_num_rows($result2) > 0) $dontshow = true;
}
// Output
if ($dontshow == false)
{
$count++;
echo "Nickname: ".$row[1]." Text: ".$row[2];
}
if ($count > 10) break;
}
Btw. I made already some improvments, so the actual question may not fit with all answers (thanks for your help till now)
Finaly its now just about to integrate the filter "dont show people listed in table "hide" for my actual user".
I think you need something along these general lines. I've done it slightly different from your question. Instead of getting the top 10 then removing records. It gets the top 10 records which would not be hidden.
SELECT c.ID, c.fromuserid, c.roomid, c.text, u.nickname
FROM chats c
JOIN user u ON c.fromuserid = u.id
where c.roomid = 3 AND user.banned = 0
AND NOT EXISTS(
SELECT * FROM hide h
WHERE h.hideuser = c.fromuserid
AND orguser = $userid)
ORDER BY c.ID DESC
LIMIT 0,10
Not tested but it would be something like:
$sql_com = "SELECT us.id, us.nickname, ch.text, ch.id ".
" FROM chats ch, ".
" user us, ".
" hide hi, ".
" banned ba, ".
" WHERE ".
" us.id != hi.hideuser ".
" us.id != ba.user ".
" us.id = ch.fromuserid ".
" AND ch.roomid = 3 ".
" ORDER BY ch.id DESC LIMIT 0,10";
Although I can't immediately find a simple way to answer your question as-is, I can point you in the right direction:
http://dev.mysql.com/doc/refman/5.0/en/subqueries.html
Using subqueries should enable you to go and select from both blocked and hidden tables, and using those in your original query.