I have a weird problem with mysql_fetch_array( $result , MYSQL_BOTH ). This is how the problem is reproduced:
Do a query string that uses LEFT JOIN, GROUP BY
select *,
(A1.total - A2.borrow_total) as remain_total
from ( select Min(sample_created_date) as created_date,
sample_model,
sample_ncc,
count(*) as total ,
sample_sort
from tb_sample
where 1=1
AND sample_kho IN ( 'hanoi','tm_hanoi' )
group by sample_model, sample_sort, sample_ncc
) A1 left join
( select sample_model as sample_model1,
count(*) as borrow_total,
sample_ncc
from tb_sample B1 left join tb_sample_ticket B2
on B1.sample_borrow_status = B2.ticket_id
where 1=1
and ticket_bo_phan_duyet = 'finish'
and ticket_status_duyet = '1'
AND sample_kho IN ( 'hanoi','tm_hanoi' )
group by sample_model, sample_ncc
) A2 on A1.sample_model = A2.sample_model1
AND A1.sample_ncc = A2.sample_ncc left join tb_product_sort A3
on A1.sample_sort = A3.sort_code
where 1=1
order by created_date DESC
LIMIT 0, 30
Implement mysql_fetch_array( $result , MYSQL_BOTH ):
$rw = mysql_fetch_array( $result , MYSQL_BOTH );
Display value of $rw:
echo $rw['sample_cc'];
But there is nothing. Then I try print_r( $rw ), see below for the result:
Array
(
[0] => 2013-06-13 04:10:39
[created_date] => 2013-06-13 04:10:39
[1] => 3G
[sample_model] => 3G
**[2] => Gmobile
[sample_ncc] =>**
[3] => 1
[total] => 1
[4] => SIM
[sample_sort] => SIM
);
I see that $Rw[2] = 3G but $rw['sample_ncc'] is null.
This is a weird problem. Please help me solve it.
Try to give explicit aliases for sample_ncc columns in subqueries A1 and A2 (e.g. sample_ncc_a1 and sample_ncc_a2) or return only one that is on the left side of LEFT JOIN (meaning in A1).
Related
Here i have two table,i have to join these two table and i have to get the plan details, i tried but is not happening, here is my code
user_info
id fullName
1 Arun
2 Sarvan
user_active_plan
id userId planName
1 1 Free Plan
2 1 Cool Plan
3 2 Free Plan
contact_property
id userId contactProperty
1 1 A
2 1 B
3 2 C
Here user_info(tablename) id (column name) i am using foreign key of user_active_plan(table name) userId(column name)
I want get the latest plan based on userId,So i am using desc order , but it is not coming expected results:
$sql = "SELECT a.fullName,b.*FROM user_info a LEFT JOIN user_active_plan b ON a.id = b.userId GROUP BY b.userId ORDER BY id DESC";
$result = $this->GetJoinRecord($sql);
print_r($result);
I am getting the following incorrect results:
Array
(
[0] => Array
(
[fullName] => Sarvan
[id] => 3
[userId] => 2
[planName] => Free Plan
)
[1] => Array
(
[fullName] => Arun
[id] => 1
[userId] => 1
[planName] => Free Plan
)
)
)
I was expecting the following:
Array
(
[0] => Array
(
[fullName] => Sarvan
[id] => 3
[userId] => 2
[planName] => Free Plan
)
[1] => Array
(
[fullName] => Arun
[id] => 2
[userId] => 1
[planName] => Coll Plan
)
)
)
Updated Expected Answer
Array
(
[0] => Array
(
[userId] => 1
[fullName] => Arun
[planId] => 2
[planName] => Cool Plan
[contactCount] => 2
)
[1] => Array
(
[userId] => 2
[fullName] => Sarvan
[planId] => 3
[planName] => Free Pla1
[contactCount] => 1
)
)
You can get the latest plan with a simple subquery, no need for grouping. The count of contacts can be done with a simple grouping:
SELECT u.id AS userId, u.fullName, p.id AS planId, p.planName, COUNT(c.userId) AS contactCount
FROM user_info u
LEFT JOIN user_active_plan p ON u.id = p.userId
LEFT JOIN contact_property c ON u.id = c.userId
WHERE p.id = (SELECT id
FROM user_active_plan
WHERE userId = u.id
ORDER BY id DESC
LIMIT 1)
GROUP BY c.userId;
You can also move the condition from the WHERE clause to the join:
SELECT u.id AS userId, u.fullName, p.id AS planId, p.planName, COUNT(c.userId) AS contactCount
FROM user_info u
LEFT JOIN user_active_plan p ON u.id = p.userId
AND p.id = (SELECT id
FROM user_active_plan
WHERE userId = u.id
ORDER BY id DESC
LIMIT 1)
LEFT JOIN contact_property c ON u.id = c.userId
GROUP BY c.userId;
I have two tables T1 1 000 records and T2 with 500 000 records. I have a query where I run a join between them and fetch data by performing some aggregations. My page seems to be loading slow. Are there any approaches to make this query faster?
I have created indexes on columns for which aggregations are being performed. I think it is a generic statement.
$query = Mymodel::selectRaw("supplier_data.name as distributor,supplier_data.name as name, supplier_data.group_id as group_id, supplier_data.pay,supplier_data.group_id as submitted_group_plan,supplier_data.group_id as group_id_string,
(SELECT sum(t.net_claim) AS trans_number
FROM transactions_data_new as t
JOIN `supplier_data` AS d ON `t`.`member_id` = `d`.`group_id`
WHERE
(
(
t.`submit_date`>= '$date_from' and t.`submit_date`<= '$date_to'
AND t.`member_id` = supplier_data.group_id
)
OR
(
(t.claim_status IS NULL)
AND
(t.submit_date is NULL)
)
)
AND d.id = supplier_data.id
) as trans_number,
(SELECT sum(t.claim) AS trans_number
FROM transactions_data_new as t
JOIN `supplier_data` AS d ON `t`.`member_id` = `d`.`group_id`
WHERE
(
(
t.`submit_date`>= '$date_from' and t.`submit_date`<= '$date_to'
AND t.`member_id` = supplier_data.group_id
)
OR
(
(t.claim_status IS NULL)
AND
(t.submit_date is NULL)
)
)
AND d.id = supplier_data.id
) as claim,
(SELECT sum(t.reversed) AS trans_number
FROM transactions_data_new as t
JOIN `supplier_data` AS d ON `t`.`member_id` = `d`.`group_id`
WHERE
(
(
t.`submit_date`>= '$date_from' and t.`submit_date`<= '$date_to'
AND t.`member_id` = supplier_data.group_id
)
OR
(
(t.claim_status IS NULL)
AND
(t.submit_date is NULL)
)
)
AND d.id = supplier_data.id
) as reversed,
(SELECT sum(t.reversal) AS trans_number
FROM transactions_data_new as t
JOIN `supplier_data` AS d ON `t`.`member_id` = `d`.`group_id`
WHERE
(
(
t.`submit_date`>= '$date_from' and t.`submit_date`<= '$date_to'
AND t.`member_id` = supplier_data.group_id
)
OR
(
(t.claim_status IS NULL)
AND
(t.submit_date is NULL)
)
)
AND d.id = supplier_data.id
) as reversal
");
I don't see the need of this too complex/repeated with same clauses and multiple sub selects for same table which can done using a single left join
SELECT
s.name AS distributor,
s.name AS name,
s.group_id AS group_id,
s.pay,
s.group_id AS submitted_group_plan,
s.group_id AS group_id_string,
SUM(t.net_claim) AS trans_number,
SUM(t.claim) AS claim,
SUM(t.reversed) reversed,
SUM(t.reversal) reversal
FROM
supplier_data s
LEFT JOIN transactions_data_new t
ON `t`.`member_id` = s.`group_id`
AND (
(
t.`submit_date` >= '$date_from'
AND t.`submit_date` <= '$date_to'
)
OR (
t.claim_status IS NULL
AND t.submit_date IS NULL
)
)
GROUP BY s.name,
s.group_id,
s.pay
As I understand it the chunk() method is for use when you need to work with a large dataset and take an action on that data chunk by chunk.
From your question, it sounds like you're performing a query then returning the data as JSON so to me, it doesn't sound like you're taking an action on your dataset that requires chunking.
If you want to break up the returned JSON data you should be instead looking at pagination.
You could apply pagination to your query like so:
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->paginate();
You can specify the size of each set by passing a number to the paginate method:
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->paginate(25);
If I've misunderstood and you did actually want to do the chunking, I believe you could do the following:
$data = Inspector::latest('id')
->select('id', 'firstname', 'status', 'state', 'phone')
->where('firstname', 'LIKE', '%' . $searchtext . '%')
->chunk(50, function($inspectors) {
foreach ($inspectors as $inspector) {
// apply some action to the chunked results here
}
});
Also, if you're returning an eloquent object it will be automatically cast to json so you don't need to perform json_encode() as far as I'm aware.
I have user_messages table with columns id, sender_id, receiver_id, message, deleted
I have to retrieve all messages like this
SELECT *
FROM
user_messages UserMessages
WHERE (
UserMessages.deleted = false
AND (
(sender_id = $loggedin_user_id AND receiver_id = $user_id)
OR
(sender_id = $user_id AND receiver_id = $loggedin_user_id)
)
ORDER BY
created DESC
Currently, I'm using this query builder
$message_by_list = $this->UserMessages->find()
->where(['UserMessages.deleted' => false])
->andWhere(function ($exp) {
return $exp->or_([
'sender_id' => $this->Auth->user('id'),
'receiver_id' => $this->Auth->user('id')
]);
})
which is generating sql query as
FROM
user_messages UserMessages
WHERE
(UserMessages.deleted = false AND (sender_id = $loggedin_user_id OR receiver_id = $loggedin_user_id))
ORDER BY
created DESC
How to write optimized ORM Query to retrieve data as above?
Edit 2 : Updated query for arilia's answer
WHERE (
(
UserMessages.deleted = false
)
AND
(
(
UserMessages.sender_id = $loggedin_user_id
AND
UserMessages.receiver_id = $user_id
)
OR
UserMessages.sender_id = $user_id
OR /// doubt here
UserMessages.receiver_id = $loggedin_user_id
)
)
try this
$message_by_list = $this->UserMessages->find()
->where(['sender_id' => $user_id, 'receiver_id' => $logged_user_id])
->orWhere(['sender_id' => $logged_user_id, 'receiver_id' => $user_id])
->andWhere(['UserMessages.deleted' => false]);
I'm strugling with this query within prestashop , i know it could be easily fixed by changing to sa.available_for_order but this way it breaks the logic of other core files ,
is there any other way around to fix this without renaming available_for_order to sa.available_for_order ,
SELECT
a.`id_product`,
b.`name` AS `name`,
`reference`,
a.`price` AS `price`,
sa.`active` AS `active`,
`newfield`,
shop.`name` AS `shopname`,
a.`id_shop_default`,
image_shop.`id_image` AS `id_image`,
cl.`name` AS `name_category`,
sa.`price`,
0 AS `price_final`,
a.`is_virtual`,
pd.`nb_downloadable`,
sav.`quantity` AS `sav_quantity`,
sa.`active`,
IF(sav.`quantity` <= 0, 1, 0) AS `badge_danger`,
sa.`available_for_order` AS `available_for_order`
FROM
`ps_product` a
LEFT JOIN
`ps_product_lang` b
ON
(
b.`id_product` = a.`id_product` AND b.`id_lang` = 1 AND b.`id_shop` = 1
)
LEFT JOIN
`ps_stock_available` sav
ON
(
sav.`id_product` = a.`id_product` AND sav.`id_product_attribute` = 0 AND sav.id_shop = 1 AND sav.id_shop_group = 0
)
JOIN
`ps_product_shop` sa
ON
(
a.`id_product` = sa.`id_product` AND sa.id_shop = a.id_shop_default
)
LEFT JOIN
`ps_category_lang` cl
ON
(
sa.`id_category_default` = cl.`id_category` AND b.`id_lang` = cl.`id_lang` AND cl.id_shop = a.id_shop_default
)
LEFT JOIN
`ps_shop` shop
ON
(
shop.id_shop = a.id_shop_default
)
LEFT JOIN
`ps_image_shop` image_shop
ON
(
image_shop.`id_product` = a.`id_product` AND image_shop.`cover` = 1 AND image_shop.id_shop = a.id_shop_default
)
LEFT JOIN
`ps_image` i
ON
(
i.`id_image` = image_shop.`id_image`
)
LEFT JOIN
`ps_product_download` pd
ON
(pd.`id_product` = a.`id_product`)
WHERE
1 AND `available_for_order` = 1
ORDER BY
a.`id_product` ASC
LIMIT 0, 50
PRESTASHOP
public function __construct()
{
parent::__construct();
$this->_select .= ',sa.`available_for_order` AS `available_for_order`, ';
$this->fields_list['sa!available_for_order'] = array(
'title' => $this->l('Available for order'),
'width' => 90,
'active' => 'available_for_order',
'filter_key' => 'sa!available_for_order',
'type' => 'bool',
'align' => 'center',
'orderby' => false
);
}
Modify your fields list to use having filter.
$this->fields_list['available_for_order'] = array(
'title' => $this->l('Available for order'),
'width' => 90,
'active' => 'available_for_order',
'filter_key' => 'available_for_order',
'havingFilter' => true,
'type' => 'bool',
'align' => 'center',
'orderby' => false
);
This will use HAVING instead of WHERE in query.
WHERE clause does not work on column aliases but HAVING does.
The column available_for_order must be in more than one of the tables. Just qualify the column name, as you do in the select:
WHERE 1 AND sa.available_for_order = 1
------------^
I am trying to get row counts returned for different tables based on user_id value. users is a table of all users with a unique column of user_id. All other tables have a corresponding user_id column to join on it with.
I would think this would be fairly easy, but for some reason I cannot get the counts to return right.
What I want to accomplish is alerts = ? and locations = ? where ? is the total number of rows in that table where user_id = 1,2,3,4,5,6,7, or 8.
$stmt = $db->prepare("
SELECT
count(t_alerts.user_id) as alerts,
count(t_locations.user_id) as locations
FROM users
LEFT JOIN
(SELECT user_id
FROM alert_logs
WHERE alert_logs.event_title LIKE '%blocked%'
) as t_alerts
on t_alerts.user_id = users.user_id
LEFT JOIN
(SELECT user_id
FROM location_logs
) as t_locations
on t_locations.user_id = users.user_id
WHERE users.user_id IN(1,2,3,4,5,6,7,8)
");
$stmt->execute();
//get results
$results = $stmt->fetch(PDO::FETCH_ASSOC);
EDIT :
A bit of a modification to eliminate the need of supplying the IN values... I use this in some other queries to only get results for 'active' users...
$stmt = $db->prepare("
SELECT
(SELECT COUNT(*)
FROM alert_logs al
WHERE event_title LIKE '%blocked%' AND al.user_id = u.user_id
) as alerts,
(SELECT COUNT(*)
FROM location_logs ll
WHERE ll.user_id = u.user_id
) as locations
FROM
( SELECT account_id, computer_id
FROM computers
WHERE account_id = :account_id
ORDER BY computer_id ASC LIMIT 0, :licenses
) as c
INNER JOIN users as u
on u.computer_id = c.computer_id
");
$binding = array(
'account_id' => $_SESSION['user']['account_id'],
'licenses' => $_SESSION['user']['licenses']
);
$stmt->execute($binding);
I am running into the problem mentioned below with this statement... it is returning an array of counts per user rather than all counts combined into one result.
Array
(
[0] => Array
(
[alerts] => 6
[locations] => 4
)
[1] => Array
(
[alerts] => 3
[locations] => 5
)
[2] => Array
(
[alerts] => 1
[locations] => 4
)
[3] => Array
(
[alerts] => 0
[locations] => 0
)
[4] => Array
(
[alerts] => 0
[locations] => 0
)
[5] => Array
(
[alerts] => 0
[locations] => 0
)
[6] => Array
(
[alerts] => 0
[locations] => 0
)
[7] => Array
(
[alerts] => 0
[locations] => 0
)
)
What can I do to 'combine' results?
The problem is that the alerts are multiplying with the locations. So, if there are 10 alerts and 5 locations, the result is 50 rows. That is what gets counted.
The easy solution is to use count(distinct):
SELECT
count(distinct t_alerts.user_id) as alerts,
count(distinct t_locations.user_id) as locations
. . .
The better solution is often to use a subquery to do the counting along each dimension, and then join the results together.
EDIT:
In your case, nested subqueries in the select might be the best approach, because the query filters on users:
SELECT (SELECT COUNT(*)
FROM alert_logs al
WHERE event_title LIKE '%blocked%' AND
al.user_id = u.user_id
) as alerts,
(SELECT COUNT(*)
FROM location_logs ll
WHERE ll.user_id = u.user_id
) as locations
FROM users u
WHERE u.user_id IN (1,2,3,4,5,6,7,8)
EDIT II:
I see, there is no group by at the end of your query. In that case, you might as well do:
SELECT (SELECT COUNT(*)
FROM alert_logs al
WHERE event_title LIKE '%blocked%' AND
al.user_id IN (1,2,3,4,5,6,7,8)
) as alerts,
(SELECT COUNT(*)
FROM location_logs ll
WHERE ll.user_id IN (1,2,3,4,5,6,7,8)
) as locations;
You don't need the users table at all.