For some reason my script takes too much time, that is why i had to add the ini_set('max_execution_time', 300); since 30 seconds being the default got me a fatal error.
I can't understand why this is happening, if i go directly into SSMS i get that query in 0 secs. what can be happening? i am running wamp with php 5.4.16 and the extension php_sqlsrv_54_ts
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
ini_set('max_execution_time', 300);
include "ChromePhp.php";
$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'Cliente';
$order = isset($_POST['order']) ? strval($_POST['order']) : 'DESC';
include "includes/db_config.php";
$conn = sqlsrv_connect(SV_NAME, $connectionInfo) OR die("Unable to connect to the database");
$sql =
"SELECT * FROM
(Select
Id
,Cliente
,Contrato
,Anexo
,SO
,NombreFlota
,(SELECT count(*) FROM LiveTest LEFT JOIN Producto ON Producto.Id=LiveTest.Producto_Id WHERE Producto.Order_Id=Orders.Id) as Hechas
,((SELECT count(*) FROM Producto WHERE Order_Id=Orders.Id) - (SELECT count(*) FROM LiveTest LEFT JOIN Producto ON Producto.Id=LiveTest.Producto_Id WHERE Producto.Order_Id=Orders.Id and RMA is null )) as Pendientes
,(SELECT count(*) FROM Producto WHERE Order_Id=Orders.Id ) as Total
FROM
Orders
WHERE
Orders.FechaPick is not null) as A
WHERE Total - Pendientes >0
ORDER BY $sort $order";
ChromePHP::log($sql);
$params = array();
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$result = array();
$res = sqlsrv_query($conn, $sql, $params, $options);
while($row = sqlsrv_fetch_object($res))
{
array_push($result, $row);
}
ChromePHP::log($result);
echo json_encode($result);
?>
try to modify your query like this:
Note : you do 'where f0b.Order_Id=f1.Id' then your left ouer join is cancelled because you test a value (that's why i have replaced by inner join), if you want null value too, you must do it : 'where f0b.Order_Id=f1.Id or f0b.Order_Id is null'
Select
Id
,Cliente
,Contrato
,Anexo
,SO
,NombreFlota
,isnull(f3.NbProdLive, 0) as Hechas
,isnull(f2.NbProd, 0) - isnull(f3.NbProdLiveRMANull, 0) as Pendientes
,isnull(f2.NbProd, 0) as Total
FROM
Orders f1
outer apply
(
select count(*) NbProd from Producto f0
where f0.Order_Id=f1.Id
) f2
outer apply
(
select count(*) NbProdLive, sum(case when RMA is null then 1 else 0 end) NbProdLiveRMANull
from LiveTest f0 INNER JOIN Producto f0b ON f0b.Id=f0.Producto_Id
where f0b.Order_Id=f1.Id
) f3
WHERE f1.FechaPick is not null and (isnull(f2.NbProd, 0) - (isnull(f2.NbProd, 0) - isnull(f3.NbProdLiveRMANull, 0))) >0
ORDER BY $sort $order
Related
this is my query working fine
$offset = ($this->uri->segment(4))?$this->uri->segment(4):0;
$limit =10;
i donn't use $user_id becoz this function uses pagination
$q = "select distinct attandence.user_id ,monthname(date), sum(is_absent) as absents,in_time,out_time , count(date) as working_days, leave_type , users.f_name ,leave_types.type ,leave_types.leave_type_id , users.l_name ,( select count(leave_type) from attandence where leave_type != 0 and user_id = 4 and month(date) = 10 ) as leave_count from attandence join users on (users.user_id = attandence.user_id) left join leave_types on(leave_types.leave_type_id = attandence.leave_type) where in_time != '' and out_time != '' ".$where_condition." GROUP BY attandence.user_id LIMIT ".$limit." OFFSET ". $offset;
$rows = $this->attandence_model->q($q);
$this->data['Details'] = $rows;
in view i just passed
<? foreach ($Details as $detail){ ?>
<? } ?>
Any help is going to be appreciated. :)
I have solution your query you can add sub query Sub query
See Example:
$q = "select distinct attandence.user_id ,monthname(date), sum(is_absent) as absents,in_time,out_time , count(date) as working_days, leave_type , users.f_name ,leave_types.type ,leave_types.leave_type_id , users.l_name, (select count(leave_type) from attandence where leave_type != 0 and user_id = 15 and month(date) = 10) as leave_count from attandence join users on (users.user_id = attandence.user_id) left join leave_types on(leave_types.leave_type_id = attandence.leave_type) where in_time != '' and out_time != '' ".$where_condition." GROUP BY attandence.user_id LIMIT ".$limit." OFFSET ". $offset;
I have 3 queries which I run which are nearly identical, the latter two have an AND condition.
Main query:
$mess = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
FROM chat_messages cm
INNER JOIN members m ON m.id =cm.userid
INNER JOIN chat_settings cs ON cs.id = cm.room_id
WHERE cm.setting_id = ?
ORDER BY cm.date DESC LIMIT 30 ) ddd
ORDER BY date ASC ");
$mess->bind_param("i", $room);
$mess->execute();
$mess->store_result();
$mess->bind_result($chatid,$chat_userid,$message,$voteup,$votedown,$date);
while($row = $mess->fetch()){
//im fetching here in my <div class='div1' >
}
Then, in the second div I have to add an AND condition:
$mess2 = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
FROM chat_messages cm
INNER JOIN members m ON m.id =cm.userid
INNER JOIN chat_settings cs ON cs.id = cm.room_id
WHERE cm.setting_id = ? AND voteup - votedown >= 5
ORDER BY cm.date DESC LIMIT 30 ) ddd
ORDER BY date ASC ");
$mess2->bind_param("i", $room);
$mess2->execute();
$mess2->store_result();
$mess2->bind_result($chatid,$chat_userid,$message,$voteup,$votedown,$date);
while($row2 = $mess2->fetch()){
//im fetching here in my <div class='div2' >
}
Lastly, in the third div I have a slightly different AND condition:
$mess3 = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
FROM chat_messages cm
INNER JOIN members m ON m.id =cm.userid
INNER JOIN chat_settings cs ON cs.id = cm.room_id
WHERE cm.setting_id = ? AND votedown - voteup >= 5
ORDER BY cm.date DESC LIMIT 30 ) ddd
ORDER BY date ASC ");
$mess3->bind_param("i", $room);
$mess3->execute();
$mess3->store_result();
$mess3->bind_result($chatid,$chat_userid,$message,$voteup,$votedown,$date);
while($row3 = $mess3->fetch()){
//im fetching here in my <div class='div3' >
}
Everything works BUT doing this near-same query seems clumsy. Is it possible to construct the same thing with only one query? I have used $mess->data_seek(0); but its not helping because I didn't add my condition to the query.
Just go for PhP to filter your data instead of triple query your database. In this case you can figure out to go for this solution because you call 3 times your query with the same parameter :
$mess3 = $mysqli->prepare(" SELECT *
FROM ( SELECT cm.id ,
cm.userid,
cm.message,
cm.voteup,
cm.votedown,
cm.date
FROM chat_messages cm
INNER JOIN members m ON m.id =cm.userid
INNER JOIN chat_settings cs ON cs.id = cm.room_id
WHERE cm.setting_id = ?
AND votedown - voteup >= 5
ORDER BY cm.date DESC LIMIT 30 ) ddd
ORDER BY date ASC ");
$mess3->bind_param("i", $room);
$mess3->execute();
$mess3->store_result();
$mess3->bind_result($chatid,$chat_userid ,$message,$voteup,$votedown ,$date);
while($row = $mess3->fetch()){
$voteup = $row['voteup'];
$votedown = $row['votedown'];
addToDiv1($row);
if( $voteup - $votedown >= 5 ) {
addToDiv2($row);
}
if( $votedown - $voteup >= 5 ) {
addToDiv3($row);
}
}
I will just give an answer based specifically on cleaning up your code. Technically you will still make the 3 calls in this scenario, but it will be cleaner because you include one function only, you don't see the script behind it.
As I mentioned, I am not an SQL aficionado so I can not give a good solution there (maybe you can use GROUP BY and perhaps an OR clause...I don't really know...). If I were to do this, I would do a function that can return all the options:
/core/functions/getChatMessages.php
function getChatMessages($settings,$mysqli)
{
$id = (!empty($settings['id']))? $settings['id'] : false;
$type = (!empty($settings['type']))? $settings['type'] : false;
$max = (!empty($settings['max']))? $settings['max'] : 30;
$mod = '';
// No id, just stop
if(!is_numeric($id))
return false;
// equation one
if($type == 'up')
$mod = ' AND voteup - votedown >= 5';
// equation two
elseif($type == 'down')
$mod = ' AND votedown - voteup >= 5';
$mess = $mysqli->prepare("SELECT * from ( SELECT cm.id ,cm.userid,cm.message,cm.voteup,cm.votedown,cm.date
FROM chat_messages cm
INNER JOIN members m ON m.id =cm.userid
INNER JOIN chat_settings cs ON cs.id = cm.room_id
WHERE cm.setting_id = ? {$mod}
ORDER BY cm.date DESC LIMIT {$max} ) ddd
ORDER BY date ASC");
$mess->bind_param("i", $id);
$mess->execute();
$mess->store_result();
$mess->bind_result($chatid, $chat_userid, $message, $voteup, $votedown, $date);
while($mess->fetch()){
$result[] = array(
'chatid'=>$chatid,
'chat_userid'=>$chat_userid,
'message'=>$message,
'voteup'=>$voteup,
'votedown'=>$votedown
);
}
// Send back the data
return (!empty($result))? $result : array();
}
To use:
// Include our handy function
require_once('/core/functions/getChatMessages.php');
// Store our id for use
$settings['id'] = 100;
// Should get 30 from first select
$voteGen = getChatMessages($settings,$mysqli);
// Should get 30 from second select
$settings['type'] = 'up';
$voteUp = getChatMessages($settings,$mysqli);
// Should get 15 from third select
// Just for the heck of it, I added in a limit settings
$settings['max'] = 15;
$settings['type'] = 'down';
$voteDown = getChatMessages($settings,$mysqli);
Now that you have these stored, just use a foreach loop to place them into your view. The good side of this is that you can call this where ever and when ever since the function only returns data. It allows you to work with the data in a view or non-view situation. Side note, I use PDO, so if there is something ineffective with the way the mysqli is fetching, that will be why. It's probably just best to fetch an assoc array to return...
I have to output some products from table 'products', along with the lowest price from the table 'product_licenses', which is the only column I need from that table in this query.
However, when I try to use the sql function MIN(), my loop only runs through the code once and gets the first result and then it stops, so I am a bit lost here.
This is the query using min() :
$mysql->query("
SELECT pd.*, min(lc.price) AS price
FROM `products` AS pd, product_licenses AS lc
WHERE pd.`status` = '1' AND lc.product_id = pd.id
ORDER BY pd.`id` ASC
$limitQuery
");
I'm using this function to get the products, but this, unfortunately, fetches the highest price:
public function getAllProducts($start = 0, $limit = 0, $order = '`datetime` ASC') {
global $mysql;
$limitQuery = '';
if ($limit != 0) {
$limitQuery = " LIMIT $start,$limit ";
}
**// Not working if I use min() on lc.price**
$mysql->query("
SELECT pd.*, lc.price
FROM `products` AS pd, product_licenses AS lc
WHERE pd.`status` = '1' AND lc.product_id = pd.id
ORDER BY pd.`id` ASC
$limitQuery
");
if ($mysql->num_rows() == 0) {
return false;
}
$this->usersWhere = '';
$return = array();
while ($d = $mysql->fetch_array()) {
$categories = explode(',', $d['category_id']);
unset($d['category_id']);
foreach ($categories as $c) {
$c = trim($c);
if ($c != '') {
$d['category_id'][$c] = $c;
}
}
$return[$d['id']] = $d;
}
$this->foundRows = $mysql->getFoundRows();
return $return;
}
Add GROUP BY in your query. your current query returns only one result since your are using aggregate function (MIN) but not grouping it.
SELECT pd.col1,
pd.col2, min(lc.price) AS PRICE
FROM `products` AS pd
INNER JOIN product_licenses AS lc
ON lc.product_id = pd.id
WHERE pd.`status` = '1'
GROUP BY pd.col1, pd.col2, pd.col3
ORDER BY pd.`id` ASC
$limitQuery
PS: post the structure of your database with records. It will the community understands your question clearly :)
You have no group by clause in your query, so the query is returning the first row only.
SELECT pd.col1, pd.col2, pd.col3, min(lc.price) AS price
FROM `products` AS pd, product_licenses AS lc
WHERE pd.`status` = '1' AND lc.product_id = pd.id
group by pd.col1, pd.col2, pd.col3
ORDER BY pd.`id` ASC
$limitQuery
You'll need a GROUP BY in there, like so:
SELECT pd.*, min(lc.price) AS price
FROM `products` AS pd, product_licenses AS lc
WHERE pd.`status` = '1' AND lc.product_id = pd.id
GROUP BY pd.`id`
ORDER BY pd.`id` ASC
$limitQuery
Note that with MySQL, you only need to group by the id column even though you are selecting other columns from the products table.
Much of the query was put in variables, but for the purpose of this question I have included them in the query. Margin is the sum of all the alias queries above it ($margin).
I am getting the error:
SQLSTATE[HY000]: General error: 1111 Invalid use of group function in company-performance-control.php on line 117
Here is my query:
$margin = $invoicesOut.'-'.$costs.'+'.$creditsIn.'-'.$creditsOut ;
$result = $dbh->query("SELECT CONCAT(MONTH(invoices_out.date), '/', YEAR(invoices_out.date)) AS theDate,
SUM(COALESCE(
(SELECT SUM(invoices_out.net/rate)
FROM invoices_out, (SELECT jobRef, invoiceRef FROM invoices_out_reference GROUP BY invoiceRef) AS unique_references
WHERE unique_references.invoiceRef = invoices_out.id
AND unique_references.jobRef = jobs.id
),
0)) AS invoiced,
SUM((SELECT SUM((quantity*parts_trading.sellingNet)/currencies.rateVsPound)
FROM parts_trading, currencies
WHERE parts_trading.sellingCurrency = currencies.id
AND parts_trading.enquiryRef = enquiries.id
AND jobs.id NOT IN
(SELECT DISTINCT jobRef FROM invoices_out_reference)
)
+
COALESCE(
(SELECT SUM(enquiries_custom_fees.feeAmountNet/currencies.rateVsPound)
FROM enquiries_custom_fees, parts_trading, currencies
WHERE enquiries_custom_fees.enquiryRef = enquiries.id
AND enquiries_custom_fees.enquiryRef = parts_trading.enquiryRef
AND parts_trading.sellingCurrency = currencies.id
),
0
)) AS pending,
SUM(COALESCE(
(SELECT SUM(net)
FROM credit_notes_supplier, parts_trading_buying
WHERE credit_notes_supplier.partRef = parts_trading_buying.id
AND parts_trading_buying.enquiryRef = enquiries.id)
,0
)) AS creditsIn,
SUM(COALESCE(
(SELECT SUM(net)
FROM credit_notes_customer, parts_trading
WHERE credit_notes_customer.partRef = parts_trading.id
AND parts_trading.enquiryRef = enquiries.id)
,0
)) AS creditsOut,
SUM(((SELECT SUM(qty*(parts_trading_buying.buyingNet/currencies.rateVsPound))
FROM parts_trading_buying, currencies
WHERE parts_trading_buying.buyingCurrency = currencies.id
AND parts_trading_buying.enquiryRef = enquiries.id
)
+
COALESCE(
(SELECT SUM(parts_trading_buying_charges.feeAmountNet)
FROM parts_trading_buying_charges, parts_trading_buying
WHERE parts_trading_buying_charges.partRef = parts_trading_buying.id
AND parts_trading_buying.enquiryRef = enquiries.id
)
, 0))) AS costs,
SUM($margin) AS margin
FROM jobs, enquiries, invoices_out,
(SELECT jobRef, invoiceRef
FROM invoices_out_reference
GROUP BY invoiceRef
) AS unique_invoice_refs
WHERE enquiries.id = jobs.enquiryRef
AND unique_invoice_refs.invoiceRef = invoices_out.id
AND jobs.id = unique_invoice_refs.jobRef
AND jobs.stateRef != 1
AND jobs.stateRef != 5
GROUP BY YEAR(invoices_out.date), MONTH(invoices_out.date)") ;
I have narrowed it down to these:
SUM($creditsIn) AS creditsIn
SUM($creditsOut) AS creditsOut
Which are defined as:
$creditsIn = 'COALESCE(
(SELECT SUM(net)
FROM credit_notes_supplier, parts_trading_buying
WHERE credit_notes_supplier.partRef = parts_trading_buying.id
AND parts_trading_buying.enquiryRef = enquiries.id)
,0
)' ;
$creditsOut = 'COALESCE(
(SELECT SUM(net)
FROM credit_notes_customer, parts_trading
WHERE credit_notes_customer.partRef = parts_trading.id
AND parts_trading.enquiryRef = enquiries.id)
,0
)' ;
What a big query!
Troubleshoot this. Simply remove each subquery one at a time and determine which query is causing this.
The following MySQL query runs in PHP without errors, but the resultset is empty. Directly outputting the query string to a file and running the query in the MySQL client using 'source [filename]' returns several rows of results, as expected.
Is there something that would cause this query not to work with PHP? categorylinks.cl_to and smw_spec2.value_string are both varbinary(255). Show create table indicates engine=InnoDB and default charset=binary.
Things I have tried without success:
$sql = preg_replace("/[\n\t]+/", " ", $sql);
Changing '_wpg' and 'Derp' to CAST('_wpg' AS BINARY(255))
Changing '_wpg' and 'Derp' to BINARY '_wpg'
I am using the MediaWiki DatabaseMysql class to execute the query and fetch rows, but it's a very thin abstraction, and I'm certain it's not the problem (see below).
SELECT
prop.name AS prop_name, prop.count AS prop_count, prop.type AS prop_type,
val.value AS val_value, val.unit AS val_unit, val.count AS val_count
FROM
(
SELECT
s_id, name, type, COUNT(foo.name) AS count
FROM (
(
SELECT
cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, spec.value_string AS type
FROM `smw_ids` s
INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_atts2` a)
ON (cl.cl_from = p.page_id AND
p.page_title = s2.smw_title AND
s2.smw_id = a.s_id AND
a.p_id = s.smw_id)
LEFT JOIN `smw_spec2` spec ON s.smw_id = spec.s_id
)
UNION ALL
(
SELECT
cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, '_wpg' AS type
FROM `smw_ids` s
INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_rels2` a)
ON (cl.cl_from = p.page_id AND
p.page_title = s2.smw_title AND
s2.smw_id = a.s_id AND
a.p_id = s.smw_id)
)
) AS foo
WHERE foo.cat_name = 'Derp'
GROUP BY name
ORDER BY count DESC
LIMIT 10
) AS prop
INNER JOIN
(
SELECT
bar.p_id AS p_id, bar.value AS value, bar.unit AS unit, COUNT(bar.value) AS count,
IF( #prev != p_id, #rownum := 1, #rownum := #rownum+1 ) AS rank,
#prev := p_id
FROM (
(SELECT a.p_id AS p_id, a.value_xsd AS value, a.value_unit AS unit FROM `smw_atts2` a)
UNION ALL
(SELECT r.p_id AS p_id, s.smw_sortkey AS value, NULL AS unit
FROM `smw_rels2` r INNER JOIN `smw_ids` s ON r.o_id = s.smw_id)
) AS bar
GROUP BY value, unit
ORDER BY count DESC
) AS val
ON prop.s_id = val.p_id
WHERE val.rank <= 50
ORDER BY prop_count DESC, prop_name, val_count DESC, val_value
Edit: The following test script outputs nothing. query.sql contains exactly the query above, written to file immediately preceding the mysql_query() call in MediaWiki's database class.
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mediawiki', $db);
$res = mysql_query(file_get_contents("query.sql"), $db);
while ($row = mysql_fetch_assoc($res)) {
var_dump($row);
}
echo mysql_error($db);
Edit: I imported a huge database dump and afterwards, when I loaded the PHP page, there was a noticeable wait that seemed to indicate that the query was running, but still no results showed. I ended up reworking the query, and I no longer have this problem.
Try this to detect and report errors better:
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mediawiki', $db);
$res = mysql_query(file_get_contents("query.sql"), $db);
if (!$res) {
print "SQL Error ".mysql_errno().":".mysql_error().", from query: '".file_get_contents("query.sql")."'";
} else {
while ($row = mysql_fetch_assoc($res)) {
var_dump($row);
}
}
Try this after mysql_connect:
mysql_query('SET NAMES utf8;');