I have a MySQL table payment where I store all the payment related data of my client. The table fields are: fileNo, clientName, billNo, billAmount, status. I want to build a search form where I will input the fileNo or the clientName, which will yield a table fetching all the records for that file number or the client name where status = 0 (unpaid). Here billAmount is a floating point number.
I am no good in MySQL, but here is my version of the SQL by fileNo
$sql = "SELECT * FROM `payment` WHERE `fileNo` = '$fileNo' AND `status` = '0'";
SQL by clientName
$sql = "SELECT * FROM `payment` WHERE `clientName` = '$clientName' AND `status` = '0'";
either way I make the query, I will also need to show the total unpaid amount against that fileNo or clientName.
From my understanding, the SUM() operation should be something like this:
$sql = "SELECT SUM(billAmount) AS `unpaid` WHERE `fileNo` = '$fileNo' AND `status` = '0'";
or
$sql = "SELECT SUM(billAmount) AS `unpaid` WHERE `clientName` = '$clientName' AND `status` = '0'";
My question is, am I correct with my SUM() operation? And how do I get the total unpaid amount that I selected as unpaid? can I store it in a variable?
Any idea, suggestions or resource link will be much appreciated!
i think u should use the second query but u missed WHEre clause
$sql = "SELECT SUM(billAmount) AS `unpaid` FROM payment WHERE `clientName` = '$clientName' AND `status` = '0'";
your total amount unpaid yes u cant get it as variable like that
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
ehco $row['unpaid'] ; <------ this will give you the unpaid amount.
of course after u fetch your query .
I think your query is quite right, don't forget the FROM clause :
$sql = "SELECT SUM(billAmount) AS `unpaid` FROM payment WHERE `clientName` = '$clientName' AND `status` = '0'";
Be careful if your client's name possibly contains quotes (have a look on addslashes function)
Then, to get the sum :
$sum = current(mysql_fetch_row(mysql_query($sql)));
BTW
In this query you have missed out FROM Clause :-)
"SELECT SUM(billAmount) AS `unpaid`
FROM payment
WHERE `fileNo` = '$fileNo'
AND `clientName` = '$clientName'
AND `status` = '0'";
Yes it is possible to store a value in a variable from first query and to be used in a secondaries.
I wish if I could give you a proper sample from SQLFIDDLE, on mobile now. Will update later if you can try this code.
Related
I would like to get lowest price of product based on last crawled dates by various resellers. My current function is very basic, it gets me lowest price from table without considering reseller ids and crawled timestamps.
I've rough idea that we can SELECT * FROM "custom_data_table" and process the data using php. Please have a look at attachment for further clarification.
function get_lowest_price($table_id) {
global $wpdb;
$table_prices = $wpdb->get_results(
$wpdb->prepare(
"SELECT price FROM `custom_data_table` WHERE tableid= %d"
,$table_id)
);
if (!empty($table_prices) && $table_prices !== NULL)
return rtrim(min($table_prices)->price, '00');
}
The right query here is:
SELECT price
FROM custom_data_name cdn, (
SELECT MAX(crawled) AS maxCrawled, resellerid
FROM custom_data_name
GROUP BY resellerid
) cdnFiltered
WHERE cdn.crawled = cdnFiltered.maxCrawled AND
cdn.resellerid = cdnFiltered.resellerid AND
tableid = %d;
Try this:
SELECT B.price
FROM (SELECT resellerid, MAX(crawled) max_crawled
FROM custom_data_table
GROUP BY resellerid) A
JOIN custom_data_table B
ON A.resellerid=B.resellerid AND A.max_crawled=B.crawled;
Maybe use ORDER BY crawled and LIMIT 1
I am trying to convert a MYSQL query to codeigniter and going no wheres real fast. I am trying to convert this query
$conn->prepare("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc");
This is in my model:
//$where = '(SELECT MAX(mix_number)from podcasts)';
$this->db->select('id,song,artist,album,track,mix_name,date, link');
//$this->db->where('mix_number', '(SELECT MAX(mix_number)from podcasts)');
$this->db->order_by('track', 'asc');
$query = $this->db->get('podcasts');
return $query->result();
My problem area is in the where statement. When I comment out the where statement I get the data. Obviously not in the manner I want it.
I am doing it this way becuase my next query(s) will be
("SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = **(SELECT MAX(mix_number) FROM podcasts) - 1** order by track asc")
And on down to (SELECT MAX(mix_number) FROM podcasts) - 3
Any thoughts on the proper way of writing the where statement? Thank you for yout time.
Set the third argument of where() to false to prevent CI from altering the string you pass in to the second argument, then you can do the subquery:
return $this->db
->select('id,song,artist,album,track,mix_name,date, link')
->where('mix_number', '(SELECT MAX(mix_number) from podcasts)', false)
->order_by('track', 'asc')
->get('podcasts')
->result();
https://www.codeigniter.com/userguide2/database/active_record.html$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
For me this produces the following query:
SELECT `id`, `song`, `artist`, `album`, `track`, `mix_name`, `date`, `link`
FROM (`podcasts`)
WHERE mix_number = (SELECT MAX(mix_number) from podcasts) ORDER BY `track` asc
If you are not too particular about using CodeIgniter's Active Record syntax, you can simply use your query as is:
$sql = "SELECT `id`,`song`,`artist`,`album`,`track`,`mix_name`,`date` FROM `podcasts` where mix_number = (SELECT MAX(mix_number) FROM podcasts) order by track asc";
$this->db->query($sql);
and then use $query->result() to get your results.
I am working on Zend Framework, got 1 problem in query, i want to fetch userid from tbl_user & want to check record with And in where clause
here is my query
$sql=$this->select()->from(users)->where('email = ? ', trim($email) , 'password = ?', md5(trim($password)) );
when i am printing query it prints
select * from users where email = 'test#gmail.com';
i want to print query like
select * from users where email = 'test#gmail.com' AND password='123456';
thanks in advance
You were close. You just need to call the where() method twice:
$this->select()->from(users)->where('email = ?', trim($email))
->where('password = ?', md5(trim($password)));
Suppose I am firing query that will upadte data if all value are available else will run with 0 rows updated. So How to get those 0 rows updated query/data from Oracle db in PHP?
In my script, I am updating table if item_flag is N, item_name is stored in $sku so checking with that & site_code is in $final_code so all these in where condition.
so if update query runs successfully with value updation then I am taking execution result in $result & updating status of Mysql table.
But what happened is when there is some data not present say $sku in update query then query runs with 0 rows updated...& likewise it will move to if($result) loop & update the status which I don't want as practically data/row is not get updated...
so How to get 0 rows updated query/data from Oracle in PHP?
--------------some code above--------
$query_ora_update = "UPDATE ITEM_DETAILS SET ITEM_FLAG= 'Y', LAST_UPDATE_DATE = sysdate WHERE ITEM_NAME = '$sku' AND SITE_CODE = '$final_code' AND ITEM_FLAG = 'N' ";
$parse_result = oci_parse($conn,$query_ora_update);
$result = oci_execute($parse_result);
oci_commit($conn);
if($result)
{
$query_update_alert = "UPDATE product_creation SET alert_status =1 where
entity_id = $entity_id and sku = '$sku' and alert_status = 0";
$result_query_update_alert = mysql_query($query_update_alert);
}
-------------------------------
Use oci_num_rows function. Documentation Here
EDIT: Example Code
$parse_result = oci_parse($conn,$query_ora_update);
$result = oci_execute($parse_result);
$row_count = oci_num_rows($parse_result);
oci_commit($conn);
if($row_count > 0)
{
-------------------
I want to do a simple SQL query in Drupal, but I'm not sure how. I would like to achieve this:
SELECT COUNT(nid) AS i_count FROM node WHERE `created` != `changed`;
I have the following code, which doesn't work:
$query = db_select('node', 'n');
$query->addExpression("COUNT(nid)","i_count");
$query->condition("created","changed","!=");
$i_number_published = $query->execute()->fetchCol();
The reason why it doesn't work is that it compares the column created with the string value "changed". Is there any way I can tell it to compare columns instead of column-string?
Use the where() member function to add conditions based on other fields in the table:
$query = db_select('node', 'n');
$query->addExpression("COUNT(nid)","i_count");
$query->where("created != changed");
$i_number_published = $query->execute()->fetchCol();
$result_handler = db_query("select count(nid) from {node} where `created` != `changed`") ;
$result_arr = db_fetch_array($result_handler) ;
$number_of_nodes = $result_arr['count(nid)'] ;