php mysql view total amount from the database - php

I want to echo sum of Amound row with student id but I have another column which is call "type" (it should be have only Admission, Installment, Other).
so when I want to echo it should be show the total of the amount column but not included (Other from "Type")
I want to echo that red box total with particulate student id

select sum(Amount) as total_amount
from `table_name`
where `Type` in('Admission', 'Installment', 'Other') group by Student_ID

Related

How to create new table for the output of DISTINCT COUNT to be distributed in rows, not in column?

My query displays the DISTINCT count of buyers with corresponding ticketserial#. I need to automatically calculate the SOLD and BALANCE column and save into the database either into the existing table (table1) with the rows that corresponds to the ticketserial. I've already exhausted my brain and did google many times but I just can't figure it out. So I tried another option by trying to create a new table into the database for the output of DISTINCT COUNT but I didn't find any sample query to follow, so that I could just use INNER JOIN for that new table with table1, with that the PRINTED, SOLD are in the same table, thus I can subtract these columns to obtain the values for the BALANCE column.
Existing table1 & table2 are records in the database via html form:
Table1
Ticket Serial Printed Copies SOLD(sold) Balance
TS#1234 50 ?(should be auto ?
TS#5678 80 ?(should be auto ?
(so on and so forth...)
Table2
Buyer Ticket Serial
Adam TS#1234
Kathy TS#1234
Sam TS#5678
(so on and so forth...)
The COUNT DISTINCT outputs the qty. of sold tickets:
<td> <?php print '<div align="center">'.$row['COUNT(ticketserial)'];?></td>
...
$query = "SELECT *, COUNT(ticketserial) FROM buyers WHERE ticketsold != 'blank' GROUP BY
ticketserial ";
It's COUNT output looks like this:
Ticket Serial------Distinct Count
TS#1234 7
TS#5678 25
(so on and so forth...)
I tried to update the SOLD column and BALANCE column by UPDATE or INSERT and foreach loop but only the first row in table was updated.
Table1
Ticket Serial Printed Copies Sold Balance
TS#1234 50 **7** 0
TS#5678 80 **0** 0
TS#8911 40 **0** 0
(so on and so forth...)
Note: The fieldname "sold" in table1 is not the same with the fieldname "ticketsold" in table2 as the former is quantity and the later is ticketserials.
Your question is a bit hard to follow. However this looks like a left join on a aggregate query:
select
t1.ticket_serial,
t1.printed_copies,
coalesce(t2.sold, 0) sold,
t1.printed_copies - coalesce(t2.sold, 0) balance
from table1 t1
left join (
select ticket_serial, count(*) sold
from table2
group by ticket_serial
) t2 on t2.ticket_serial = t1.ticket_serial
If you are looking to update the main table:
update table1 t1
left join (
select ticket_serial, count(*) sold
from table2
group by ticket_serial
) t2 on t2.ticket_serial = t1.ticket_serial
set
t1.sold = coalesce(t2.sold, 0),
t1.balance = t1.printed_copies - coalesce(t2.sold, 0)
I would not actually recommend storing the sold and balance in the main table - this is derived information that can be easily computed when needed, and would be tedious to maintain. If needed, you could create a view using the first above SQL statement, which will give you an always up-to-date perspective at your data.

Select rows from 2 tables in 1 query

Need to select name,domain from table 1, than I need a sum of values from a column from table 2 with condition table1.id = table2.
Table 2 does not have the same number of columns.
I have tried joining 2 queries with UNION and UNION ALL, but i keep getting the same problem of different number of columns.
$rows = $test->query('select domain,name from customers
UNION
select SUM(customer_id) from main.rentals,main.customers where customer_id = customers.id');
Expected would be "User's Name" - "domain" - "number of rentals(integer)"
Warning: SQLite3::query(): Unable to prepare statement: 1, SELECTs to the left and right of UNION do not have the same number of result columns
If you use SUM, it will sum all ids, for example if you have a customer ID of 10 and it shows 5 times the result will be 50. In the other hand if you use count it will count the rows that that id was shown. That's why we are doing a group by. You may tweak it to fill your specific needs, but this is one way to achieve what you want.
$query = "
SELECT
domain,
name,
count(customer_id) as Total
FROM
customers
left join main.rentals on customers.id = customer_id
GROUP BY
customer_id
";
$test->query($query );

select top 5 sellers in mysql

I have the follow table in MySQL
TABLE "SALES"
id, product, code, quantity, amount, who-sold-it
while the field "who-sold-it" is just a example to understand the question but not the real name of the field
Now I have another table the name is "USERS" and it looks like this
id, name, sellercode
I need to get the top 5 sellers using the 2 tables looking the who-sold-it in each SALE and display their name and sold amounts
Order the results by total sales and take the top 5 with limit. You can also join the sales table with the seller table to get the name of the seller.
select users.name, users.sellercode, sum(sales.amount) as total
from sales, users
where sales.sellercode = users.sellercode
group by users.sellercode, users.name
order by total desc
limit 5
To display the result:
<?php while ($row = mysqli_fetch_assoc($result)) ?>
<tr>
<td><?php echo htmlspecialchars($row['name'])</td>
<td><?php echo htmlspecialchars($row['total'])</td>
</tr>
<?php } ?>

MySQL and PHP fetch particular student who pass the exam

MySQL table name: Student
Fields: id,name,subject1,subject2,subject3.
Need to filter out the students who has more than 150 marks in total.
Use this query:
SELECT id, (SUM(subject1)+SUM(subject2)+SUM(subject3)) AS total FROM student GROUP BY id
You can check if(total > 150) in mysql query..

MySQL query printing double records

All recorders in sales & purchase table are only entered Once. I have checked it carefully. However I wish to combine these two tables in such a way that both tables will be completely fetched. here is my query
note that timestamp column has mysql DATE format
Select
sales.ID as sid,
sales.saleHatsh,
sales.timestamp as sdate,
sales.gatePass as sGP,
sales.pname as sPN,
sales.description as sDES,
sales.balance as sbal,
purchase.ID as pid,
purchase.purchaseHatsh,
purchase.timestamp as pdate,
purchase.gatePass as pGP,
purchase.pname as pPN,
purchase.description as pDES,
purchase.balance as pbal
from sales,purchase
where sales.timestamp='2013-11-11' OR purchase.timestamp='2013-11-11'
here is the result of my query & sales & purchase table
Sales table only have 2 recorder
Purchase table only has 4 recorder
What is happening there is that you are not joining those tables in any way. so you are getting all the possible matches from those tables. Looking at the columns i don't think you want to JOIN, but probably you want a UNION instead:
SELECT
sales.ID AS id,
sales.saleHatsh AS hatsch,
sales.TIMESTAMP AS date,
sales.gatePass AS GatePass,
sales.pname AS pname,
sales.description AS Description,
sales.balance AS balance,
'SALE' AS transanctionType
FROM sales
WHERE sales.TIMESTAMP = '2013-11-11'
UNION
SELECT
purchase.ID,
purchase.purchaseHatsh,
purchase.TIMESTAMP,
purchase.gatePass,
purchase.pname,
purchase.description,
purchase.balance,
'PURCHASE'
FROM purchase
WHERE purchase.TIMESTAMP = '2013-11-11'
I added a column "transactionType" for you to identify which ones are sales or purchases.

Categories