I am having three tables pcn_type, in_e_s_s__p_c_ns, p_c_n_details. I am trying to concat three different values into single using group concat.
My Query:
SELECT 'browser' AS NAME, CONCAT( '[', CONCAT('{"', pcn_type.name, '",',
COUNT(JPN_ID), '}'), ']' ) AS DATA FROM p_c_n_details INNER JOIN
in_e_s_s__p_c_ns RIGHT OUTER JOIN pcn_type ON pcn_type.name =
p_c_n_details.type AND in_e_s_s__p_c_ns.pcnid=
p_c_n_details.JPN_ID GROUP BY pcn_type.name
Result got:
NAME | DATA
-------------------------------------
browser [{"Design Change",4}]
browser [{"EOL",10}]
browser [{"Process Change",21}]
Expecting Result:
NAME | DATA
--------------------------------------------------------------------
browser [{"Design Change",4},{"EOL",10},{"Process Change",21}]
How to restructure the above query to get the expected result.
use GROUP_CONCAT function
select name,GROUP_CONCAT(DATA SEPARATOR ' ')
from
(
SELECT 'browser' AS NAME, CONCAT( '[', CONCAT('{"', pcn_type.name, '",',
COUNT(JPN_ID), '}'), ']' ) AS DATA FROM p_c_n_details INNER JOIN
in_e_s_s__p_c_ns RIGHT OUTER JOIN pcn_type ON pcn_type.name =
p_c_n_details.type AND in_e_s_s__p_c_ns.pcnid=
p_c_n_details.JPN_ID GROUP BY pcn_type.name
) as T group by name
Related
My SQL query
SELECT DISTINCT a.*,categories_2625729.title AS category_title,categories_2625729.id AS category,c.id as cid, c.title, '' as `introtext`, 'com_hpj_content.ad' as content_type, '' as banner, '' as `fulltext`, '' as sheettext, c.publish_up, c.publish_down, c.price, c.advertiser_id, c.seller_id, c.sold, c.soldupdated,0 AS is_favorite
FROM `jos_hpj_content_item` AS a
LEFT JOIN jos_categories AS categories_2625729 ON categories_2625729.id = a.category
LEFT JOIN jos_hpj_content_item_ad AS cff ON cff.id = a.ref_id AND a.content_type = 'com_hpj_content.ad'
LEFT JOIN jos_fields_values AS fields_values_min_maxyear ON fields_values_min_maxyear.item_id = a.ref_id AND fields_values_min_maxyear.field_id=8
LEFT JOIN jos_fields_values AS fields_values_min_max_2mileage ON fields_values_min_max_2mileage.item_id = a.ref_id AND fields_values_min_max_2mileage.field_id=10
LEFT JOIN jos_hpj_content_item_ad c ON c.`id` = a.`ref_id` AND a.`content_type`='com_hpj_content.ad'
WHERE a.state = 1 AND (`c`.title LIKE '%22%' OR c.`introtext` LIKE '%22%' OR a.synonym LIKE '%22%') AND a.content_type IN ('com_hpj_content.ad') AND a.ref_id IN (53,307,353,354,572,644,717,934,978,1056,1086,1128,1149,1199,1276,1294,1314,1324,1347,1351,1396,1411,1462,1470,1513,1537,1740,1741,1836,1861,1916,1988,1989,2005,2006,2124,2158,2266,2267,2268,2277,2511,2575,2577,2582,2585,2587,2654,2815,2894,2906,2975,3021,3179,3194,3219,3224,3239,0) AND a.state = 1
UNION (SELECT name FROM jos_users WHERE name LIKE '%22%')
I'm getting error
#1222 - The used SELECT statements have a different number of columns
If I remove UNION the sql query return result but if I keep UNION the error return
I'm trying to search the number in different tables in c.title or in name from jos_user and return the result
As #danblack points out in comments, the error
#1222 - The used SELECT statements have a different number of columns
is saying that there's a different number of columns on the two SELECT statement. In
SELECT DISTINCT a.*,
categories_2625729.title AS category_title,
categories_2625729.id AS category,c.id as cid,
c.title,
'' as `introtext`,
'com_hpj_content.ad' as content_type,
'' as banner,
'' as `fulltext`,
'' as sheettext,
c.publish_up,
c.publish_down,
c.price,
c.advertiser_id,
c.seller_id,
c.sold,
c.soldupdated,0 AS is_favorite
FROM ...
..you're select a lot more columns than in this
SELECT name
FROM ...
The number of columns of those two SQL statements have to match in order for UNION to be able to add the results of the second query to the result of the first.
So this will work:
SELECT DISTINCT c.title
FROM ...
UNION
SELECT name
FROM ...
because now both individual SELECT statements return just one column.
I am new at mysql. I have code but it is displaying byte array why ?
Sorry about my english :(
SELECT p.city_id,p.name city, CONCAT("[",
GROUP_CONCAT(CONCAT('{id:"', a.id, '", address:"', a.address, '"}')),
"]") As sahalar
FROM cities AS p LEFT JOIN carpet_fields As a ON(p.city_id = a.city_id)
GROUP BY p.city_id, p.name ORDER BY p.city_id
it must be show json data but. The result is
Wrong result
city_id name sahalar
1 ADANA 0x5B7B69643A223236222C20616464726573733A2248757A75
Correct result
city_id name sahalar
1 ADANA [{id:"1A", address:"Huzurevleri Mah.77232 Sk. ADANA / ÇUKUROVA"},{id:"1B", address:"KARSLILAR MAH. 82008. SOK. / MAHFESIGMAZ"}]
I found the solution
SET SESSION group_concat_max_len = 1000000;
SELECT p.city_id,p.name city,
CONCAT("[",
GROUP_CONCAT( CONCAT('{id:"', CAST(a.id as char(10)), '", address:"', a.address, '"}') ),
"]") As sahalar
FROM cities AS p
LEFT JOIN carpet_fields As a ON(p.city_id = a.city_id)
GROUP BY p.city_id, p.name ORDER BY p.city_id
If I got 2 table, which is
How do I get the result of
I know combine the name is using CONCAT_WS function, but I don't understand how can I exchange the row such as the "wife" and "husband" on my output.
You should join the client table two time (of of each related part in row) using tablename alias
select
concat(c1.client_firstname, ' ' , c1.client_lastname) as A_Name
, r.rel_client1_state as A_State
, concat(c2.client_firstname, ' ' , c2.client_lastname) as B_Name
, r.rel_client2_state as B_State
from relationships as r
inner join client as c1 on r.rel_client1_id = c1.client_id
inner join clinet as c2 on r.rel_client2_id = c2.client_id
SELECT CONCAT_WS(' ', c1.client_firstname, c1.client_lastname) as c1name,
r.rel_client1_state, CONCAT_WS(' ', c2.client_firstname, c2.client_lastname) as c2name,
r.rel_client2_state FROM client c1
JOIN relationship r ON c1.client_id = r.rel_client1_id
JOIN client c2 ON c2.client_id = r.rel_client2_id WHERE 1
i have select in query
IFNULL(GROUP_CONCAT(DISTINCT CONCAT(ED.dependent_name, ED.date_of_birth))," No Dependents ") AS Dependents
The Result i get is like this..
Data is fine but problem with this is, it look untidy, i want to have date of birth in brackets
e-g for Employee E-02, i want record something like this, means enclose the date in small braces.
Muhammad Zubair (1998-12-15) ,Amir Khan (2000-12-15)
Is there any way i can update the above select statement and get the result like i want to have or any other better way to achieve a good looking result.?
MY Query:
SELECT
`E`.`employee_code` AS Employee_Code,
E.full_name AS NAME,
E.father_name AS Father_Name,
IFNULL(
GROUP_CONCAT(
DISTINCT CONCAT( ED.dependent_name '(', ED.date_of_birth, ')')), " No Dependents " ) AS Dependents
FROM
(`employee` E)
INNER JOIN `employee_project` EP
ON `EP`.`employee_id` = `E`.`employee_id`
INNER JOIN `permanant_contacts` PC
ON `PC`.`employee_id` = `E`.`employee_id`
INNER JOIN `ml_district` MLD
ON `MLD`.`district_id` = `PC`.`district`
LEFT JOIN `dependents` ED
ON `ED`.`employee_id` = `E`.`employee_id`
AND ED.trashed = 0
WHERE `E`.`trashed` = 0
GROUP BY `E`.`employee_id`
Have youy tried
CONCAT(ED.dependent_name, '(', ED.date_of_birth, ')')
Try
CONCAT(ED.dependent_name,CONCAT('(',CONCAT(ED.date_of_birth, ')')))
I'm trying retrieve data using a LEFT JOIN and GROUP_CONCAT(). However, where values aren't present, I need something to represent the lack of data, or a method of identification for each, so that I can do something with them in the application.
CONCAT_WS(0x1D,
GROUP_CONCAT(DISTINCT bcod1.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod2.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod3.value
SEPARATOR 0x1F),
GROUP_CONCAT(DISTINCT bcod4.value
SEPARATOR 0x1F)) AS clients_options
Here, the four instances of GROUP_CONCAT() relate to either an INNER JOIN or a LEFT JOIN in a search performed by the user:
INNER JOIN
bookings_clients_options bco1 ON (bco1.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod1 ON (bco1.name = 'unit_code')
AND (bco1.bookings_client_option_id = bcod1.bookings_client_option_id)
AND (bcod1.value REGEXP ('.*'))
AND (bcod1.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco2 ON (bco2.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod2 ON (bco2.name = 'creditor_ev_number')
AND (bco2.bookings_client_option_id = bcod2.bookings_client_option_id)
AND (bcod2.value REGEXP ('.*'))
AND (bcod2.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco3 ON (bco3.client_id = '3')
LEFT JOIN
bookings_clients_options_data bcod3 ON (bco3.name = 'purchase_order_number')
AND (bco3.bookings_client_option_id = bcod3.bookings_client_option_id)
AND (bcod3.booking_attendee_id = bookings_attendees.booking_attendee_id)
INNER JOIN
bookings_clients_options bco4 ON (bco4.client_id = '3')
INNER JOIN
bookings_clients_options_data bcod4 ON (bco4.name = 'purchase_order_booking')
AND (bco4.bookings_client_option_id = bcod4.bookings_client_option_id)
AND (bcod4.value REGEXP ('Y'))
AND (bcod4.booking_attendee_id = bookings_attendees.booking_attendee_id)
Here, bcod3.value is where the results "collapse" in that if there's no value for bcod3.value but a value for bcod4.value, then bcod4.value drops into the space for bcod3.value.
As you can see, each of these columns has a name, but having tried...
GROUP_CONCAT(DISTINCT bcod" . $x . ".value SEPARATOR 0x1F) AS unit_code
... I got an error from the surrounding CONCAT_WS().
I tried...
IF(bcod" . $x . ".value = '', 'QQQ', GROUP_CONCAT(DISTINCT bcod" . $x . ".value SEPARATOR 0x1F))
... but that doesn't appear to do anything.
I tried...
GROUP_CONCAT(DISTINCT IF(bcod" . $x . ".value = 0, 'QQQ', bcod" . $x . ".value) SEPARATOR 0x1F)
.. and while it did grab the instances of bcod3.value, it also grabbed some of the instances of bcod1.value that have values which are not '0'.
I'm not 100% sure I understand your question, but I think I do. Sample data and desired results are worth 1000 words of explanation.
If the problem is that NULL or blank values are being ignored, then just do:
CONCAT_WS(0x1D,
coalesce(GROUP_CONCAT(DISTINCT bcod1.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod2.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod3.value SEPARATOR 0x1F), ''),
coalesce(GROUP_CONCAT(DISTINCT bcod4.value SEPARATOR 0x1F), '')
) AS clients_options
It is generally considered a benefit that group_concat() and concat_ws() ignore NULL values. However, if you want to do something with them, just use coalesce().