1 user has multiple records in tbl_activity and there is diplay_name, I would like to concat all of them il a columns name activity_name
SELECT *
FROM `tbl_user`
INNER JOIN `tbl_user_authassignment`
ON `tbl_user`.`id` = `tbl_user_authassignment`.`userid`
INNER JOIN `tbl_activity`
`tbl_activity`.`id` AS 'activityid'
ON `tbl_user`.`id` = `tbl_activity`.`vendor_id`
WHERE `tbl_user_authassignment`.`itemname` = 'vendor'
In MySQL (which is suggested by your syntax), you can use group_concat(). I don't know which fields you care about but it is something like this:
SELECT u.*, group_concat(a.acitvityid) as activityids
FROM `tbl_user` u INNER JOIN
`tbl_user_authassignment` ua
ON u.`id` = ua.`userid` INNER JOIN
`tbl_activity` a
ON u.`id` = a.`vendor_id`
WHERE ua`.`itemname` = 'vendor'
GROUP BY u.id;
Looks like it's MySQL syntax and so you can use GROUP_CONCAT() for this purpose
SELECT tu.*,
xx.`id` AS 'activityid',
xx.activity_name
FROM `tbl_user` tu
INNER JOIN `tbl_user_authassignment` tua
ON tu.`id` = tua.`userid`
INNER JOIN (
select `vendor_id`,
`id`,
group_concat(diplay_name) as activity_name
from `tbl_activity`
group by `vendor_id`) xx ON tu.`id` = tua.`vendor_id`
WHERE tua.`itemname` = 'vendor';
Related
Please check the below code.
SELECT
`order`.idorder
, order_status_code.idorder_status_code
, order_status_code.order_status_code
, user.iduser
, `order`.required_delivery_date
, `order`.cancel
, `order`.date_created
, `order`.last_updated
, COUNT(order_item.idorder_item)
from
`order`
INNER JOIN order_status_code
ON `order`.idorder_status_code = order_status_code.idorder_status_code
INNER JOIN user
ON `order`.iduser = user.iduser
INNER JOIN order_item
ON order_item.idorder = `order`.`idorder`
WHERE
`order`.iduser = 1
In here, I want the COUNT(order_item.idorder_item) to return the number of items under the idorder. In other words, if I run that SQL Part along, that would be like below
SELECT
COUNT(`idorder_item`)
from
order_item
where
idorder = 1
How can I get this done in my main query?
SELECT `order`.idorder,
order_status_code.idorder_status_code,
order_status_code.order_status_code,
user.iduser,
`order`.required_delivery_date,
`order`.cancel,
`order`.date_created,
`order`.last_updated,
COUNT(order_item.idorder_item),
(SELECT COUNT(`idorder_item`)
from order_item
where idorder=1) as count_idorder_item
from `order`
INNER JOIN order_status_code ON `order`.idorder_status_code = order_status_code.idorder_status_code
INNER JOIN user ON `order`.iduser = user.iduser
INNER JOIN order_item ON order_item.idorder = `order`.`idorder`
WHERE `order`.iduser= 1
I have following sql statement:
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
I using INNER JOIN on multi-database ,so that you can see sc and master_hankoya is difference databases
When I run it ,I get error :
Unknown column 'g.goods_id ' in 'on clause'
You can see g alias for table master_hankoya.goods,and goods_id is a column in this
I guess that I have problem with INNER JOIN
Please help me correct it
Update : I check again and take a silly problem ,have a special character in query make it failed to run
try this
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (g.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN
(select * from master_hankoya.goods ) g
ON ha.WEBHINID = g.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
Don't use alias name for master_hankoya.goods like
INSERT INTO wk1_tbl (shohin_code, shohin_mei, variation_flag)
SELECT ha.HINCD, ha.HINNMA, if (master_hankoya.goods.goods_para_id IS NULL, 0, 1) AS variation
FROM ( SELECT KOSHINCD, count(HINCD) AS quatity
FROM sc.HINMTF
GROUP BY KOSHINCD ) AS group_set
INNER JOIN sc.HINMTA ha
ON ha.HINCD = group_set.KOSHINCD
INNER JOIN master_hankoya.goods
ON ha.WEBHINID = master_hankoya.goods.goods_id
WHERE ha.HINKB = '2' and ha.DATKB <> '9';
INNER JOIN sc.HINMTA ha
Change this line to:
INNER JOIN HINMTA AS ha
INNER JOIN master_hankoya.goods g
Change that line to
INNER JOIN goods AS g
The syntax for setting an alias for a table is:
table_name AS table_alias
I have the following SQL. I am trying to perform a GROUP_CONCAT within a Join but no matter how I seem try it doesnt seem to work right.
Essentially there is a link tag table that does a one to many on another table that holds tags. So there might be say 8 tags for one article record. I need them concatenated together .vs. ending up with 8 records.
SELECT
`articles`.`art_title`,
`articles`.`art_bodytext`,
`info`.`inf_start_date`,
`info`.`inf_end_date`,
`info`.`inf_hits`,
`info`.`inf_acl`,
`category`.`name`,
`options`.`opt_tpl`,
`options`.`opt_published`,
`options`.`opt_options`,
`options`.`opt_acl`,
`tags`.`tag`
FROM
`linktable`
INNER JOIN `articles` ON (`linktable`.`lnk_data_id` = `articles`.`art_id`)
INNER JOIN `info` ON (`articles`.`art_info_id` = `info`.`inf_id`)
INNER JOIN `category` ON (`articles`.`art_cat_id` = `category`.`id`)
AND (`articles`.`art_cat_tree_id` = `category`.`fid`)
INNER JOIN `options` ON (`articles`.`art_opt_id` = `options`.`opt_id`)
INNER JOIN `linktags` ON (`articles`.`art_tag_set_id` = `linktags`.`lnk_tagset_id`)
LEFT OUTER JOIN GROUP_CONCAT(`tags`) ON (`linktags`.`lnk_tag_id` = `tags`.`tag_id`)
WHERE
`linktable`.`lnk_pgc_id` = 1
Move the GROUP_CONCAT() into the field list, it is not a join condition, but a field calculation.
Add a GROUP BY to find the rows to combine
This gives
SELECT
`articles`.`art_title`,
`articles`.`art_bodytext`,
`info`.`inf_start_date`,
`info`.`inf_end_date`,
`info`.`inf_hits`,
`info`.`inf_acl`,
`category`.`name`,
`options`.`opt_tpl`,
`options`.`opt_published`,
`options`.`opt_options`,
`options`.`opt_acl`,
GROUP_CONCAT(`tags`.`tag`) AS tags
FROM
`linktable`
INNER JOIN `articles` ON (`linktable`.`lnk_data_id` = `articles`.`art_id`)
INNER JOIN `info` ON (`articles`.`art_info_id` = `info`.`inf_id`)
INNER JOIN `category` ON (`articles`.`art_cat_id` = `category`.`id`)
AND (`articles`.`art_cat_tree_id` = `category`.`fid`)
INNER JOIN `options` ON (`articles`.`art_opt_id` = `options`.`opt_id`)
INNER JOIN `linktags` ON (`articles`.`art_tag_set_id` = `linktags`.`lnk_tagset_id`)
LEFT JOIN `tags` ON (`linktags`.`lnk_tag_id` = `tags`.`tag_id`)
WHERE
`linktable`.`lnk_pgc_id` = 1
GROUP BY `articles`.`art_id`
SELECT IFNULL(oea.`name`,op.name) AS "Consultation"
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE oea.employee_profile_id IN (
SELECT profile_id
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id
FROM e op
WHERE op.status
) AND
ca.status = 1
I am getting this error
Unknown column 'op.name' in 'field list'
Is there any possibility to use op.name outside the scope?
JOIN the subquery in the WHERE clause instead of using the IN predicate. This way you can select the name from it.
But you didn't select the column name from in the subquery. You selected only the profile_id, you have to select it with the profile_id. Something like:
SELECT
IFNULL(oea.`name`, op.name) AS Consultation
FROM `a` ca
JOIN `b` oea ON ca.`consultation_id` = oea.id AND oea.status = 1
INNER JOIN
(
SELECT profile_id, name
FROM d pp
WHERE pp.status='1'
UNION
SELECT op.id, name
FROM e op
WHERE op.status
) AS op ON oea.employee_profile_id = op.profile_id
LEFT OUTER JOIN c oeal ON oea.`location_id` = oeal.id
WHERE ca.status = 1;
I assumed that the two tables have the name column, if one of them didn't have it, you have to use null instead.
This is my current query:
$sel = "SELECT
db1t1.userid, db1t1.customer_id, db2t1.customers_id, db2t1.orders_id, db2t2.products_price
FROM
database1.table1 db1t1
LEFT JOIN database2.table1 db2t1 ON
db1t1.customer_id = db2t1.customers_id
LEFT JOIN database2.table2 db2t2 ON
db2t1.orders_id = db2t2.orders_id
WHERE db1t1.userid IN(
SELECT
l.userid, l.username, r.username, r.cus_id
FROM
database1.table3 l
LEFT JOIN database2.table4 r ON
l.username = r.username
WHERE r.cus_id = '1234'
)";
Error message:
Operand should contain 1 column(s)
The error occurred because that you returned a result with multiple columns to an IN clause.
Try this:
SELECT
`db1t1`.`userid`, `db1t1`.`customer_id`, `db2t1`.`customers_id`,
`db2t1`.`orders_id`, `db2t2`.`products_price`
FROM `database1`.`table1` AS `db1t1`
LEFT JOIN `database2`.`table1` AS `db2t1`
USING (`customers_id`)
LEFT JOIN `database2`.`table2` AS `db2t2`
USING (`orders_id`)
WHERE `db1t1`.`userid` IN (
SELECT `l`.`userid`
FROM `database1`.`table3` AS `l`
LEFT JOIN `database2`.`table4` AS `r`
USING (`username`)
WHERE `r`.`cus_id` = 1234
)
What are you trying to achieve ? Maybe we can find a better solution.
Also, I think that you should do an INNER JOIN instead of a LEFT JOIN in the subquery.