I have a PHP code ready which will fetch data from a mysql database,The PHP code will also display the data in tabular form.
What I want to Achieve:
I have a dummy database named "stackoverflow" with two tables "customer" & "receipt" both of these tables have a field called "date" with datatype as date as well. full schema here
I want some selected entries from these tables to appear in an html table. the entries should be sorted by date in increasing order.
What I have tried and accomplished:
The data is fetched and shown in tabular form successfully, but date wise sorting is partially complete.
output of my result (without php code since jsfiddle doesn't support php)
I have used PHP to compare dates between dates of each table and then show the respective data
<?php
while( $kb = mysql_fetch_assoc($customer) ){
customer_data($kb); //function which echos <tr>'s of customer table
while($cd = mysql_fetch_assoc($reciept)){ //function which echos <tr>'s of reciept table
if($cd['date'] <= $kb['date']){
reciept_data($cd);
break;
}
}
}
?>
Problems I am facing:
The sorting is not exactly like what I wanted. (only some of them are as per increasing date order).
link to my whole project file with database schema and sample data (just import into phpmyadmin) for reference: http://www.mediafire.com/?bcbvkn8uq89ya6n
If you are still confused about what kind of output I am trying to get.I made a table in excel here's the Image:
I am struggling with this problem since a couple of days. I think this is due to my lack of sql skills?. Any help would be appreciated.
Thanks!
UPDATE:
This is how I pickup data from the tables using php:
$customer = mysql_query("SELECT * FROM customer WHERE customer_name = '$name' ORDER BY date ASC ");
$reciept = mysql_query("SELECT * FROM reciept WHERE name = '$name' ORDER BY date ASC");
After some research, I came across the UNION operator which joins two select statements, and I developed this query:
SELECT DATE, customer_name, grand_total
FROM customer
WHERE customer_name = 'JHON'
UNION ALL
SELECT DATE, name, recd_amount
FROM reciept
WHERE name = 'JHON'
but I could only select equal no. of columns in both select statements, and how do I order by date?