<?php
$today=date('Y-m-d');
$sorgu = mysqli_query($conn, "SELECT * FROM urun");
while ($sira = mysqli_fetch_array($sorgu)) {
$urun = $sira['urun'];
$query = mysqli_query($conn, "SELECT product_name, birim, SUM(quantity) FROM
product WHERE product_name = '$urun' AND quantity != '0' AND quantity > '0'
AND grup!='uygulama' AND skt > '$today' GROUP BY product_name, birim ");
while ($row = mysqli_fetch_array($query)){
if ($row['2'] <= $sira['minstok']) {
echo count($row['0']);
} } } ?>
This code belongs to a table which has a notification system. I want the notification part increase when the line number increases in that table but it turns 11111111... , So how can I make the code give me sum?
Incidentally, everything up to while ($row... can be rewritten as follows:
SELECT p.product_name
, p.birim
, SUM(p.quantity) total
FROM urun u
JOIN product p
ON p.product_name = u.urun
AND p.quantity > 0
AND p.grup != 'uygulama'
AND p.skt > CURDATE()
GROUP
BY p.product_name
, p.birim;
Related
I have this table
The table contains amount with values that has both negative and positive signs. I was able to fetched the sum of positive amount but after the sumation I have zero values which I don't want to show - it fetched all values.
Objective: fetch sum of amount and return amount with no zero values.
What I tried so far
$ques = "select * from company";
$checks22y = sqlsrv_query($conn, $ques);
$row22y = sqlsrv_fetch_array($checks22y, SQLSRV_FETCH_ASSOC);
$daty = $row22y['BRSES_DATE']->format('Y-m-d H:m:i');
$com = $row22y['branch'];
$query = "SELECT distinct ".$limitresult." Member.Branch,Member.GL_No,Member.Ac_NO,Member.BRANCH+Member.GL_NO+Member.AC_NO
AS BRGLAC,Customer.Cust_No,Customer.Name,Group_Name,ID_CARD,Subgroup as subgroup2,
Cust_Type,Cust_Sex,Cust_Cat,Area_Code,Cust_Type,Dobirth,Address,Ref_No,Bank_VNO,Cust_Ca2,
nType,Group_Code FROM Member INNER JOIN CUSTACC ON Member.Branch = CustAcc.Branch AND
Member.GL_NO = CustACC.GL_No AND Member.AC_NO = CustACC.AC_No
INNER JOIN Customer ON Member.Branch = Customer.Branch ".$branchid." AND Member.Cust_No = Customer.Cust_No ".$accnos." WHERE
CUSTACC.Exp_Date < '$daty' AND MEMBER.Gl_NO IN (SELECT Coa.GL_NO FROM Coa WHERE Product = 'S' ) AND cust_type IN ('IND','GRP','MEM') ";
$check = sqlsrv_query($conn, $query);
$i = 1;
while($rows = #sqlsrv_fetch_array( $check, SQLSRV_FETCH_ASSOC)) {
$ac = $rows['Ac_NO'];
$br = $rows['Branch'];
//Second sub query
$get ="select ac_no, gl_no, SUM(amount) as OutBalance,MAX(Batch_Date) AS Last_Trx2 from memtrans where gl_no like '2001%'
and ac_no='$ac' group by ac_no, gl_no HAVING SUM(amount) > 0
";
$check2 = sqlsrv_query($conn, $get);
$rowb = sqlsrv_fetch_array( $check2, SQLSRV_FETCH_ASSOC);
//Third sub query, OD history
$od = "select od_limit from custacc where ac_no='$ac' and od_limit > '0'";
$odc = sqlsrv_query($conn, $od);
$rowbo = sqlsrv_fetch_array( $odc, SQLSRV_FETCH_ASSOC);
}
The outcome
If you look at the Outstanding Balance you will see zero values appearing.
The secon sub query is where the fetching of Outstanding Balance took place.
Just solved it.
Here is the query I later used:
SELECT distinct ".$limitresult." member.Branch,Member.GL_No,Member.Ac_NO,Member.BRANCH+Member.GL_NO+Member.AC_NO
AS BRGLAC,Customer.Cust_No,Customer.Name,Group_Name,ID_CARD,Subgroup as subgroup2,Cust_Type,Cust_Sex,Cust_Cat,Area_Code,Cust_Type,Dobirth,Address,Ref_No,Bank_VNO,Cust_Ca2,nType,Group_Code, SUM(memtrans.amount) as OutBalance,MAX(memtrans.Batch_Date) AS Last_Trx2,company.BRSES_DATE, company.branch, CustACC.od_limit FROM Member INNER JOIN CUSTACC ON Member.Branch = CustAcc.Branch AND Member.GL_NO = CustACC.GL_No AND Member.AC_NO = CustACC.AC_No INNER JOIN Customer ON Member.Branch = Customer.Branch AND Member.Cust_No = Customer.Cust_No INNER JOIN memtrans ON memtrans.ac_no = member.ac_no INNER JOIN company ON company.branch = member.branch ".$branchid." ".$accnos." WHERE CUSTACC.Exp_Date < company.BRSES_DATE AND CUSTACC.od_limit > '0.00' AND MEMBER.Gl_NO IN (SELECT Coa.GL_NO FROM Coa WHERE Product = 'S' ) AND cust_type IN ('IND','GRP','MEM') group by memtrans.ac_no, memtrans.gl_no,company.branch, member.branch, member.gl_no,Member.ac_no,
Customer.cust_no, Customer.name, Customer.group_name,Customer.id_card,Customer.subgroup,Customer.cust_type,
Customer.cust_sex, Customer.cust_cat,Customer.area_code,Customer.dobirth,Customer.address,Customer.ref_no, Customer.Bank_VNO,Customer.cust_ca2,Customer.ntype,Customer.group_code,company.BRSES_DATE,CUSTACC.od_limit HAVING SUM(memtrans.amount) > 0)
Thanks guys. From a grateful heart.
This is a small section of code i've been working with.
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->here i want percentage</td>";
echo "</tr>";
}
?>
What I would like to do is display the percentage of total/target in the final column of my table. Not sure what the best way to do this is and would be keen to hear some ideas for this.
I did try doing it just as total/target in the echo but thinking it probably needs to be evaluated earlier as a variable or something? Whatever i tried didn't work anyway...
Looked at this but not sure how to implement it in my scenario - MSSQL display percentage calculated from two columns
Try this,
<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp, (ifnull(t.total,0)/target)*100 as present FROM `members` as m
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");
while($row = mysql_fetch_object($sql))
{
echo "<tr>";
$rank = $rank + 1;
echo "<td><center>$rank</center></td>";
echo "<td>$row->username</td>";
echo "<td>$row->total</td>";
echo "<td>$row->target</td>";
echo "<td>$row->present</td>";
echo "</tr>";
}
?>
Here is logic
((cast("A/B" as float)/"C") * 100.00) AS "D%"
all value must be integer and when you are dividing them it must be convert as a float so calculation goes correctly.
Hope it will work for you.
I'm trying to order a list of items based on the amount of comments for each topic as shown below:
$page = $_GET['page'];
$query = mysql_query("SELECT * FROM topic WHERE cat_id='$page' LIMIT $start, $per_page");
if (mysql_num_rows($query)>=1)
{
while($rows = mysql_fetch_array($query))
{
$number = $rows['topic_id'];
$title = $rows['topic_title'];
$description = $rows['topic_description'];
//get topic total
$sqlcomment = mysql_query("SELECT * FROM comments WHERE topic_id='$number'");
$commentnumber = mysql_num_rows($sqlcomment);
// TRYING TO ORDER OUTPUT ECHO BY TOPIC TOTAL ASC OR DESC
echo "
<ul>
<li><h4>$number. $title</h4>
<p>$description</p>
<p>$topictime</p>
<p>$commentnumber</p>
</li>
</ul>
";
}
}
else
{
echo "<p>no records available.</p><br>";
}
What would be the best way to order each echo by $num_rows (ASC/DESC values)? NOTE: I've updated with the full code - I am trying to order the output by $commentnumber
The first query should be:
SELECT t.*, COUNT(c.topic_id) AS count
FROM topic AS t
LEFT JOIN comments AS c ON c.topic_id = t.topic_id
WHERE t.cat_id = '$page'
GROUP BY t.topic_id
ORDER BY count
LIMIT $start, $per_page
You can get $commentnumber with:
$commentnumber = $rows['count'];
You don't need the second query at all.
First of all you have error here
echo "divs in order from least to greatest "number = $num_rows"";
It should be
echo "divs in order from least to greatest number = " . $num_rows . "";
And about the most commented try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY column DESC/ASC";
Or if there is not count column try with
$sql = "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY COUNT(column) DESC/ASC";
I was hoping you could help me figure this out. I would like to run a query that would search a table and all it's rows and the date matches the current month. Then return the user_id. What I am trying to achieve is gathering the top 5 user id's under this query.
How can I change this to find the top 5 for the current month? I am usually able to figure out my queries but this one has me stumped.
This is all I have at the moment.
$top = mysqli_query($mysqli, "SELECT sum(amount) AS total FROM sales WHERE status = 'S' AND MONTH(date) = 1");
while($row = mysqli_fetch_row($top))
{
$userid = $row['0'];
}
Update:
$top = mysqli_query($mysqli, "SELECT user_id
FROM
(
SELECT user_id, SUM(amount) total
FROM sales
WHERE status = 'S'
AND MONTH(date) = MONTH(CURDATE())
AND YEAR(date) = YEAR(CURDATE())
GROUP BY user_id
) q
ORDER BY total DESC
LIMIT 5");
while($row = mysqli_fetch_row($top))
{
$topsales[] = $row['0'];
}
foreach($topsales as $topsale) {
echo $topsale;
}
Are you looking for a query like this?
SELECT user_id
FROM
(
SELECT user_id, SUM(amount) total
FROM sales
WHERE status = 'S'
AND MONTH(date) = MONTH(CURDATE())
AND YEAR(date) = YEAR(CURDATE())
GROUP BY user_id
) q
ORDER BY total DESC
LIMIT 5
or just
SELECT user_id
FROM sales
WHERE status = 'S'
AND MONTH(date) = MONTH(CURDATE())
AND YEAR(date) = YEAR(CURDATE())
GROUP BY user_id
ORDER BY SUM(amount) DESC
LIMIT 5
Here is SLQFiddle demo
Note: using MONTH() and YEAR() functions will prevent MySql from using any index you might have on date column
Now your php code might look like this
$db = new mysqli('localhost', 'user', 'password', 'dbname');
if ($db->connect_errno) {
die('Cannot connect'); // TODO: better error handling
}
$sql = "SELECT user_id
FROM sales
WHERE status = 'S'
AND MONTH(date) = MONTH(CURDATE())
AND YEAR(date) = YEAR(CURDATE())
GROUP BY user_id
ORDER BY SUM(amount) DESC
LIMIT 5";
$topsales = array();
if ($query = $db->prepare($sql)) {
$query->execute();
$query->bind_result($user_id);
while ($query->fetch()) {
$topsales[] = $user_id;
}
$query->close();
} else {
die('Unable to prepare: ' . $db->error); // TODO: better error handling
}
$db->close();
foreach ($topsales as $topsale) {
echo $topsale . '<br>';
}
I want to show all the price but not the max and mini.
I'm using NOT IN but it's not working.
<?php $cod_product = $_GET["cod_product"];
$sql = "SELECT `ppm`
,`price`
,`market`
,`product`
,`name_market`
,`cod_market`
FROM ppm, markets
WHERE product=$cod_product AND cod_market=market NOT IN (SELECT MAX(price), MIN(price) FROM ppm)
ORDER BY price ASC";
$result = mysql_query($sql, $connection) or die("fail");
if ($dados = mysql_fetch_array($result)) {
do {
$cod_market = $dados['market'];
$nome_mercado = $dados['name_market'];
$price = $dados['price'];
echo $price;
echo $name_market;
}
while($dados = mysql_fetch_array($result));
}else { }
?>
Change mini() to min() (assuming MySQL).
mini() isn't a function.
Also, some of your SQL doesn't make much sense.
SELECT cod_ppm
,price
,market
,product
FROM ppm
WHERE price NOT IN (SELECT MAX(price), MIN(price) FROM ppm)
ORDER BY price ASC