UNION in MYSQL and merge columns - php

I am fetching data from two tables in mysql. I have query like this:
SELECT SUM(IF(debit IS NULL, 0, debit)) - SUM(IF(credit IS NULL, 0, credit)) as amount, th.country_id, cl.currency_name
FROM account_treasury_hq th
LEFT JOIN system_country_list cl ON cl.country_id=th.country_id
WHERE th.company_id='$company_id'
GROUP BY th.country_id
UNION
SELECT SUM(CASE WHEN ce_type = 'IN' or ce_type is NULL then payment_amount
WHEN ce_type = 'OUT' then - payment_amount
END) as amount,
source_country_id as country_id, cl.currency_name
FROM customer_payment_options cpo
LEFT JOIN system_country_list cl ON cl.country_id=cpo.source_country_id
WHERE cpo.company_id='$company_id'
GROUP BY cpo.source_country_id
Result
Array
(
[0] => Array
(
[amount] => -345
[country_id] => 40
[currency_name] => Canadian dollar
)
[1] => Array
(
[amount] => 93
[country_id] => 210
[currency_name] => Sri Lankan rupee
)
[2] => Array
(
[amount] => 46.5
[country_id] => 236
[currency_name] => United States dollar
)
[3] => Array
(
[amount] => 916249.8999999999
[country_id] => 40
[currency_name] => Canadian dollar
)
[4] => Array
(
[amount] => -6670.1
[country_id] => 210
[currency_name] => Sri Lankan rupee
)
[5] => Array
(
[amount] => 2017.67
[country_id] => 236
[currency_name] => United States dollar
)
)
In the result array, you can see the same country_id. I want to merge those same country id with 1. It means, there should only be each amount information for each country_id.
(amount should be added by adding multiple countries)
Is there any way to do it in PHP or mysql itself?
Thanks for any kind of help.
Required Result
Array
(
[0] => Array
(
[amount] => 915904.89
[country_id] => 40
[currency_name] => Canadian dollar
)
[1] => Array
(
[amount] => -6577.1
[country_id] => 210
[currency_name] => Sri Lankan rupee
)
[2] => Array
(
[amount] => 2064.17
[country_id] => 236
[currency_name] => United States dollar
)
)

just select it all in an outer query sum the amount and group by country_id
SELECT
SUM(amount) country_id, currency_name
FROM
( SELECT
SUM(IF(debit IS NULL, 0, debit)) - SUM(IF(credit IS NULL, 0, credit)) as amount,
th.country_id,
cl.currency_name
FROM account_treasury_hq th
LEFT JOIN system_country_list cl ON cl.country_id=th.country_id
WHERE th.company_id='$company_id'
GROUP BY th.country_id
UNION
SELECT
SUM(CASE
WHEN ce_type = 'IN' or ce_type is NULL then payment_amount
WHEN ce_type = 'OUT' then - payment_amount
END
) as amount,
source_country_id as country_id,
cl.currency_name
FROM customer_payment_options cpo
LEFT JOIN system_country_list cl ON cl.country_id=cpo.source_country_id
WHERE cpo.company_id='$company_id'
GROUP BY cpo.source_country_id
) t
GROUP BY country_id

You're doing a union, with grouping in the individual subquery. Each subquery in a union has NO awareness of what the other parallel queries are doing. Each query's group by will apply only to that ONE query.
You'd need to do the heavy duty work in a wrapper query, e.g.
SELECT SUM(.....)
FROM (
SELECT th.country_id AS country_id, etc...
UNION ALL
SELECT source_country AS country_id, etc...
) AS foo
GROUP BY country_id
e.g. do all the grouping and "math" in the outer query, and the inner queries simply retrieve all the records you do want grouped together.

Didn't test this (would need a sqlfiddle to play with) but you'd want to aggregate them in the query. Something like this (you'll want to tweak the group by and select for your needs):
SELECT SUM( x.amount ), x.country_id, x.currency_name
FROM
(
SELECT SUM(IF(debit IS NULL, 0, debit)) - SUM(IF(credit IS NULL, 0, credit)) as amount, th.country_id, cl.currency_name
FROM account_treasury_hq th
LEFT JOIN system_country_list cl ON cl.country_id=th.country_id
WHERE th.company_id='$company_id'
GROUP BY th.country_id
UNION
SELECT SUM(CASE WHEN ce_type = 'IN' or ce_type is NULL then payment_amount
WHEN ce_type = 'OUT' then - payment_amount
END) as amount,
source_country_id as country_id, cl.currency_name
FROM customer_payment_options cpo
LEFT JOIN system_country_list cl ON cl.country_id=cpo.source_country_id
WHERE cpo.company_id='$company_id'
GROUP BY cpo.source_country_id
) AS x
GROUP BY x.amount, x.country_id, x.currency_name

Related

select GROUP_CONCAT in mysql

I have two tables:
table1:
id email
1 john#gmail.com
2 doe#gmail.com
table2:
userid key value
1 firstname john
1 phone 112233
2 firstname doe
2 phone 223344
This is mysql query without condition:
SELECT a.*,
b.*,
GROUP_CONCAT(b.key),
GROUP_CONCAT(b.value)
FROM table1 a
LEFT JOIN table2 b ON a.id = b.userid
This is result:
array(
[id] => 1
[email] => john#gmail.com
[userid] => 1
[key] => firstname,phone
[value] => john,112233
)
array(
[id] => 2
[email] => doe#gmail.com
[userid] => 2
[key] => firstname,phone
[value] => doe,223344
)
This is mysql query with condition:
SELECT a.*,
b.*,
GROUP_CONCAT(b.key),
GROUP_CONCAT(b.value)
FROM table1 a
LEFT JOIN table2 b ON a.id = b.userid
WHERE b.key = "firstname"
AND b.value LIKE '%jo%'
And this is result:
array(
[id] => 1
[email] => john#gmail.com
[userid] => 1
[key] => firstname
[value] => john
)
But I want this:
array(
[id] => 1
[email] => john#gmail.com
[userid] => 1
[key] => firstname,phone
[value] => john,112233
)
There any way to do this? thank for any help!
Your queries are lacking the GROUP BY clause to get a row per user. Then use a HAVING clause to make sure the aggregated row includes a firstname '%jo%':
SELECT a.*,
GROUP_CONCAT(b.key),
GROUP_CONCAT(b.value)
FROM table1 a
LEFT JOIN table2 b ON a.id = b.userid
GROUP BY a.id
HAVING sum(b.key = 'firstname'
AND b.value LIKE '%jo%') > 0;
true results in 1, false in 0 in MySQL. So checking whether the sum is greater than zero means checking whether the condition is true at least once.

Order by not working with group by clause

I am using corequery of mysql in cakephp. I want the records in descending order. This is my table structure
enter code here
$coreQueryUser = $this->Message->query(
"select * from messages where messages.list_id = 3
group By (if(sender_id > reciever_id, sender_id, reciever_id)),
(if(sender_id > reciever_id, reciever_id, sender_id))
order by id desc
"
);
I want last message that belongs to (sender_id and reciver_id and viceversa) that belongs to list id 3
when i run this query i get the following output
<pre>Array
(
[0] => Array
(
[messages] => Array
(
[id] => 1
[sender_id] => 21
[reciever_id] => 10
[list_id] => 3
[message] => hello
[add_date] => 2016-09-25 00:00:00
[is_check] => 0
)
)
[1] => Array
(
[messages] => Array
(
[id] => 3
[sender_id] => 22
[reciever_id] => 10
[list_id] => 3
[message] => hello s
[add_date] => 2016-09-25 16:39:41
[is_check] => 0
)
)
)
but i wnat result like that:
Array
(
[0] => Array
(
[messages] => Array
(
[id] => 2
[sender_id] => 10
[reciever_id] => 21
[list_id] => 3
[message] => hello sir
[add_date] => 2016-09-25 00:00:00
[is_check] => 0
)
)
[1] => Array
(
[messages] => Array
(
[id] => 6
[sender_id] => 22
[reciever_id] => 10
[list_id] => 3
[message] => new
[add_date] => 2016-09-25 16:39:41
[is_check] => 0
)
)
)
Can anyone help me :(
The problem is that your query is against the sql standard because you have several fields in the select list that are neither in the group by list, nor are subject of an aggregate function, such as sum(). MySQL unfortunately allows such invalid queries to run under certain sql mode settings (the default settings of the most recent versions of MySQL would prevent such queries from running).
As MySQL documentation on group by clause says (bolding is mine):
If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard
SQL use of GROUP BY permits the select list, HAVING condition, or
ORDER BY list to refer to nonaggregated columns even if the columns
are not functionally dependent on GROUP BY columns. This causes MySQL
to accept the preceding query. In this case, the server is free to
choose any value from each group, so unless they are the same, the
values chosen are indeterminate, which is probably not what you want.
Furthermore, the selection of values from each group cannot be
influenced by adding an ORDER BY clause. Result set sorting occurs
after values have been chosen, and ORDER BY does not affect which
value within each group the server chooses.
You apparently want the latest record (with max(id) for each group. The proper way is to have a subquery that returns the max(id) per group and in the outer query join back to your main table using the ids to get the value of the other fields:
select m.*
from
messages m
inner join (
select max(id) as maxid
from messages
where messages.list_id = 3
group By (if(sender_id > reciever_id, sender_id, reciever_id)),
(if(sender_id > reciever_id, reciever_id, sender_id))
) t1 on m.id=t1.maxid
This code working:
SELECT * FROM generate_invoice
WHERE id IN
(
SELECT max(id) as id
FROM generate_invoice
GROUP by pay_id
ORDER by id DESC
)
How to group by DESC order
Try
SELECT * FROM ( SELECT * FROM generate_invoice ORDER BY id DESC ) AS g GROUP BY g.pay_id
OR
Use this code
SELECT m1.*,m2.* FROM generate_invoice m1 LEFT JOIN generate_invoice m2 ON (m1.pay_id = m2.pay_id AND m1.id < m2.id ) order by m1.id desc

SQL group inner join cannot get all results

I really trying to get this SQL to work. Im no expert so really cant figure this out.
$sqlquery = " SELECT
s.searchword AS searchword,
s.id AS id,
COUNT( c.id ) AS searchresult,
s.region AS region
FROM search_words AS s
INNER JOIN company_data AS c ON
c.branch_text LIKE CONCAT( '%', s.searchword, '%' )
GROUP BY 1 ORDER BY s.date DESC";
This gives me:
Array
(
[0] => Array
(
[searchword] => WHOLESALE
[searchid] => 427
[searchresult] => 98
[region] => stockholm
)
[1] => Array
(
[searchword] => cars
[searchid] => 426
[searchresult] => 26
[region] =>
)
[2] => Array
(
[searchword] => Retail
[searchid] => 342
[searchresult] => 41
[region] => stockholm
)
[3] => Array
(
[searchword] => Springs
[searchid] => 339
[searchresult] => 4
[region] => stockholm
)
[4] => Array
(
[searchword] => Leasing
[searchid] => 343
[searchresult] => 2
[region] => stockholm
)
[5] => Array
(
[searchword] => Food
[searchid] => 340
[searchresult] => 37
[region] => stockholm
)
)
But, it does not give me any of the other results where there are no searchhits, would retur something like [searchresult] => 0. Meaning they do not group as I wish, because there are no such searchwords within the company_data table.
How can I fix this, please help :(
EDIT:
Here is the full code:
public function getUserSearches()
{
$sqlquery = " SELECT
s.searchword AS searchword,
s.id AS id,
s.userId AS userid,
COUNT( c.id ) AS searchresult,
s.region AS region
FROM search_words AS s
INNER JOIN company_data AS c ON
c.branch_text LIKE CONCAT( '%', s.searchword, '%' )
GROUP BY 1 ORDER BY s.date DESC";
// IS THERE ANYTHING WRONG HERE?? LIKE IT DOES NOT MATCH AGAINST THE USER?
$result = $this->dbh->query($sqlquery, array(":userId" => $this->user_id));
$arr = array();
foreach ($result as $item) {
array_push($arr, array('searchword' => $item['searchword'], 'searchid' => $item['id'],
'searchresult' => $item['searchresult'], 'userid' => $item['userid'],
'region' => $item['region']));
}
return json_encode($arr);
return print_r($arr);
}
Use LEFT JOIN so that any row that doesn't match the search criteria in the join condition will come with null, therefore count will be 0. Something like this:
SELECT
s.searchword AS searchword,
s.id AS id,
s.region AS region,
COUNT(COALESCE(c.id, 0)) AS searchresult
FROM search_words AS s
LEFT JOIN company_data AS c
ON c.branch_text LIKE CONCAT( '%', s.searchword, '%' )
GROUP BY s.searchword, s.id, s.region
ORDER BY s.date DESC;
See this for more details about the SQL Join types.
You need a LEFT JOIN, instead of an INNER JOIN. However, there are other simplifications you can make as well:
SELECT s.searchword, s.id, COUNT( c.id ) AS searchresult, s.region
FROM search_words s INNER JOIN
company_data c
ON c.branch_text LIKE CONCAT('%', s.searchword, '%')
GROUP BY 1
ORDER BY s.date DESC
For instance, you don't need column aliases when you are not changing the column name. You are only aggregating on searchword; I assume this is unique in search_words. Otherwise, s.id, s.region. and s.date would have indeterminate values.
The use of LIKE for the JOIN affects performance. If you only have a small amount of data, this is fine. Otherwise, you might want to think about other data structures.
I think what you want is perhaps this:
public function getUserSearches()
{
$sqlquery = " SELECT
s.searchword AS searchword,
s.id AS id,
s.userId AS userid,
COUNT( c.id ) AS searchresult,
s.region AS region
FROM search_words AS s
WHERE s.userId='"+$this->userid+"'
LEFT JOIN company_data AS c ON
c.branch_text LIKE CONCAT( '%', s.searchword, '%' )
GROUP BY 1 ORDER BY s.date DESC";
// IS THERE ANYTHING WRONG HERE?? LIKE IT DOES NOT MATCH AGAINST THE USER?
$result = $this->dbh->query($sqlquery);
$arr = array();
foreach ($result as $item) {
array_push($arr, array('searchword' => $item['searchword'], 'searchid' => $item['id'],
'searchresult' => $item['searchresult'], 'userid' => $item['userid'],
'region' => $item['region']));
}
return json_encode($arr);
return print_r($arr);
}

How to get the most favourited items, from another table?

So, there's the User model, and the Item model. It's a many-to-many relation: an item can belong to many users, and a user can have many items. Therefore, there's the UserItemRel model.
To summarize:
item
id
name
date_created
date_updated
user
id
email
password
date_created
date_updated
user_item_rel
user_id
item_id
date_created
My query, before making the switch to Yii2, was this:
SELECT COUNT(UIR.`user_id`) as `favourited`, IT.`id`, IT.`name`, CA.`name` as `category`
FROM `user_item_rel` UIR
LEFT JOIN `item` IT ON UIR.`item_id` = IT.`id`
LEFT JOIN `category_item` CI ON UIR.`item_id` = CI.`item_id`
LEFT JOIN `category` CA ON CI.`category_id` = CA.`id`
WHERE UIR.`date_created` >= (SYSDATE() - INTERVAL 3 YEAR)
GROUP BY UIR.`item_id`
ORDER BY
`favourited` DESC
LIMIT 20
I've used the yii2-enhanced-gii extension to generate the models.
I want to show the 20 most favourited items in the past 48 hours, with their counts. I'm migrating from Yii1.1, and it's been quite the ride so far, and I can't figure this out.
I've found
$this->hasMany(UserItemRel::className(), ['id' => 'user_id'])
->viaTable('user_item_rel', ['id' => 'item_id'], function ($query) {
$query->andWhere(['date_created < INTERVAL 2 DAY'])
->orderBy(['COUNT(*)' => SORT_DESC]);
});
}
but how to properly use this?
The query would something like bellow. I would try to run a native query instead of trying to find out how this could be done withing the orm.
SELECT item_id, item.name, count(*) favorite
FROM user_item_rel
LEFT JOIN user ON user.id = user_item_rel.user_id
LEFT JOIN item ON item.id = user_item_rel.item_id
WHERE user_item_rel.date_created >= (sysdate() - interval 2 DAY)
GROUP BY item_id, name
ORDER BY favorite DESC
LIMIT 20
You might be able to try something like this:
$items = UserItemRel::find()
->asArray()
->select("COUNT(`user_id`) as favourited, `item_id`")
->groupBy("item_id")
->joinWith("item")
->orderBy("favourited DESC")
->indexBy("item_id")
->where("'date_created' >= '".date("Y-m-d", strtotime("-2 days"))."'")
->limit(3)
->all();
In my testing it gives me something like this:
Array
(
[1] => Array
(
[favourited] => 4
[item_id] => 1
[item] => Array
(
[id] => 1
[name] => Donec
[date_created] => 2015-08-26
[date_updated] => 2015-08-26
)
)
[8] => Array
(
[favourited] => 3
[item_id] => 8
[item] => Array
(
[id] => 8
[name] => Tellus
[date_created] => 2015-08-26
[date_updated] => 2015-08-26
)
)
[7] => Array
(
[favourited] => 2
[item_id] => 7
[item] => Array
(
[id] => 7
[name] => Mollis
[date_created] => 2015-08-26
[date_updated] => 2015-08-26
)
)
)

How to select Distinct on inner join?

Can help me, I have something problem about query select distinct, :
My table
table_pemesan
id_pms
table_pesan
no_psn
id_pms
table_dpesan
no_psn
My query mysql
select pm.id_pms,
pm.nama,
pm.date_create,
ps.no_psn,
ps.status,
dps.no_dpsn
FROM pemesan as pm
INNER JOIN pesan ps on ps.id_pms=pm.id_pms
INNER JOIN dpesan dps on dps.no_psn=ps.no_psn
ORDER BY pm.id_pms ASC
//output
Array
(
[0] => Array
(
[id_pms] => 1
[nama] => Isnan
[date_create] => 2014-05-28 23:54:54
[no_psn] => 1
[status] => sedang diproses
[no_dpsn] => 1
)
[1] => Array
(
[id_pms] => 1
[nama] => Isnan
[date_create] => 2014-05-28 23:54:54
[no_psn] => 1
[status] => sedang diproses
[no_dpsn] => 2
)
)
the array index 0 & 1 the same data, and my question how to distinct the query? thanks b4, sorry my english not good..
Try to use this query:-
SELECT DISTINCT pm.id_pms, pm.nama, pm.date_create, ps.no_psn, ps.status, dps.no_dpsn
FROM pemesan as pm
INNER JOIN pesan ps on ps.id_pms=pm.id_pms
INNER JOIN dpesan dps on dps.no_psn=ps.no_psn
ORDER BY pm.id_pms ASC

Categories