PHP MySQL Hierarchy Data group_concat - mysql

any help would be amazing. currently got the following MySQL query:
SELECT
GROUP_CONCAT( sp.`slug` SEPARATOR '/' )
FROM
`category` sn, `category` sp
WHERE
sn.lft BETWEEN sp.`lft` AND sp.`rgt`
AND sn.`id` =3
ORDER BY
sp.`lft` ASC;
Without the group_concat function this returns the desired results in the correct order. as soon as I apply group_contact the order is mashed up and incorrect. Does anyone know how to rewrite the query to give me the concatenated result desired and in the correct order?
Im at a loss on this. On a side note does anyone know how to rewrite it using the "inner join" statements as opposed to the two table names quoted next to each other as I dont understand how its joining the two tables.

Try this:
SELECT
GROUP_CONCAT( sp.`slug` ORDER BY sp.`lft` ASC SEPARATOR '/' )
FROM
`category` sn,
INNER JOIN `category` sp
ON sn.lft BETWEEN sp.`lft` AND sp.`rgt`
WHERE
sn.`id` =3

Related

CONCAT() inside GROUP_CONCAT() with count

I am trying to get results with group_concat, concat and count functions in MySQL but it gives me error.
here is my table
First, when I try to get count and status with concat, it works fine.
$query="SELECT CONCAT(`status`,':',count(status)) FROM `mytable` GROUP BY status"
output:
Hold:2
Completed:3
Cancelled:2
It's all fine till here. Now I want this output in one row. So I tried using GROUP_CONCAT().
$query="SELECT GROUP_CONCAT(CONCAT(`status`,':',count(status)) SEPARATOR ',') as rowString FROM `mytable` GROUP BY status"
but now its giving me error " Invalid use of group functions"
Note: the same query works if I replace count(status) with some other field from table ( without count). The count() function is causing some problem when used in this manner.
Desired Output
Hold:2,Completed:3,Cancelled:2
Appreciate your help.
You cannot nest aggregation functions (count() is in the arguments to group_conat()). One solution is to select from a nested subquery.
SELECT group_concat(status, ':', count SEPARATOR ',') rowstring
FROM (SELECT status,
count(*) count
FROM mytable
GROUP BY status) x;

Mysql query concate data into group concate

Here is my table. I have made the following query and get the result:
mysql - Query:
SELECT
GROUP_CONCAT(CONCAT('', j0.rent_agree_id,'-',j0.rent_sche_id) ) AS agreement_ref_no
FROM
rent_provision_history AS j0
GROUP BY provision_date
Result:
But I want to see the result differently.
For example for the last result row i am getting now: 4-68,4-69,6-107,6-108,6-109
But I want to see it like that:
4(68,69)|5(107,108,109)
How can i do that?
You need two levels of aggregation:
SELECT provision_date,
GROUP_CONCAT(rent_agree_id, '(', rsi, ')' SEPARATOR '|') agreement_ref_no
FROM (SELECT j0.rent_agree_id, GROUP_CONCAT(j0.rent_sche_id) as rsi
FROM rent_provision_historyj0
GROUP BY j0.provision_date, j0.rent_agree_id
) j0
GROUP BY j0.provision_date;

Concatenation is not working in select query

We are fetching list of ordered products from database including join with order table.
We want to list all orders with how many quantity of products order in each order on the basis of passed product ids. We also want to display customer name which was placed the order. So, as per our knowledge we have created an query to get items as:
SELECT
`main_table`.*,
`order`.*,
SUM(main_table.qty_ordered - main_table.qty_canceled) AS `custom_qty`,
SUM(main_table.row_total) AS `custom_row_total`,
SUM(main_table.tax_amount) AS `tax_amount`,
SUM(main_table.hidden_tax_amount) AS `hidden_tax_amount`,
SUM(main_table.discount_amount) AS `discount_amount`,
CONCAT(order.customer_firstname, ' ' ,order.customer_middlename, ' ', order.customer_lastname) AS full_name
FROM `sales_flat_order_item` AS `main_table`
INNER JOIN `sales_flat_order` AS `order` ON main_table.order_id=order.entity_id
WHERE (((((main_table.product_id = '902') OR (main_table.product_id = '903') OR (main_table.product_id = '904'))))) AND (main_table.store_id = '1') AND (CONCAT(order.customer_firstname, order.customer_middlename, order.customer_lastname) like '%rag%')
GROUP BY `main_table`.`sku`
All the aggregate functions used in above query working fine except concat(). Every time we will get the value of full_name column as NULL even we have name the corresponding concatenated columns.
Please any one helps me to figure out why this is not working. Are we doing something wrong in the above query?
Thanks in advance.
As CONCAT() returns NULL if any argument is NULL I'm guessing maybe one of the three arguments is NULL?
Try using the CONCAT_WS() function instead (as you use separators anyway) which skips null values.
CONCAT_WS(' ', order.customer_firstname, order.customer_middlename, order.customer_lastname) AS full_name
See the documentation for more information.
On a side note: you might want to look into how you can use table aliases to shorten the query text and make it more readable.
Are you sure EVERY column has an value? (Middlename?)
The MYSQL manual says:
SELECT CONCAT('My', NULL, 'QL')
-> NULL

Condition as a column named using AS "name" in mysql

I am trying to count line that has columns which been repeated (their value) more than x times in mysql, every time I try to execute this query I get an error :
SELECT DISTINCT
idLogin, count(idLogin ) AS "cou"
FROM
`products` AS t
GROUP BY
idLogin
HAVING
t.cou > 2
why this error is happening
Use this one
SELECT DISTINCT idLogin, count(idLogin ) AS cou FROM `products`
GROUP BY idLogin HAVING cou > 2
you can put that expression count(idLogin ) directly in having clouse....
it will not give any error.. and will be more understable as well....
or else you can do one thing -
select idLogin,cou from (
SELECT DISTINCT idLogin, count(idLogin ) AS cou
FROM products
GROUP BY idLogin ) t
where t.cou >2
Remove the double quotes from the aliased column name.
MySQL uses single back-quotes for escaping table and column names, not double quotes.
The correlation name t does not have a column cou. Just use this:
HAVING cou > 2
PS: DISTINCT is redundant in your query. The GROUP BY ensures there will only be one row per distinct value of idLogin.

mysql 'Unknown column' error

What is wrong with this:
SELECT *,
GROUP_CONCAT(DISTINCT w.tag_word ORDER BY w.tag_word ASC SEPARATOR ' ') AS tags,
MATCH (title, description, tags) AGAINST ('london') AS score
FROM article G
JOIN tag_index I ON G.article_id = I.tag_target_id
JOIN tag_word W ON I.tag_word_id = W.tag_word_id
WHERE I.tag_type_id = 1 AND MATCH (title, description, tags) AGAINST ('london')
GROUP BY G.article_id
I get the error 'Unknown column 'tags' in 'field list''
UPDATE:
Thank you Parrots for pointing out that I need the HAVING clause. I still can not figure out how to implement it. I can only guess it can not be done in one query and needs to be a subquery.
Since "tags" a value you're creating using GROUP_CONCAT you need to use the having clause. Whenever you want to apply a condition to stuff after the grouping, use having. It works the same as where just after the grouping.
Where in your code example is trying to be applied to filter the results from article that will eventually be grouped to build things like "tags".