I have this query:
SELECT c.id, p.id, p.register_date, o.office_agency_name_1, a.street_address, a.supplemental_address_1, a.city, s.abbreviation, co.iso_code, a.postal_code, ph.phone, pf.phone, e.email,
ab.name, c.addressee_custom, (Select phone from civicrm_phone where contact_id = civicrm_contact.id AND is_billing = 1) as billing_phone
FROM civicrm_contact c,
civicrm_participant p,
civicrm_value_office_info_1 o,
civicrm_address a,
civicrm_state_province s,
civicrm_country co,
civicrm_phone ph,
civicrm_phone pf,
civicrm_email e,
civicrm_address ab
WHERE p.contact_id = c.id
AND p.is_test = 0
AND p.event_id = 1
AND p.status_id NOT IN (4,11,12)
AND ( c.is_deleted = 0 OR c.is_deleted IS NULL )
AND o.entity_id = c.id
AND a.contact_id = c.id
AND s.id = a.state_province_id
AND co.id = a.country_id
AND ph.contact_id = c.id
AND ph.is_primary = 1
AND pf.contact_id = c.id
AND pf.phone_type_id = 3
AND e.contact_id = c.id
AND e.is_primary = 1
AND ab.contact_id = c.id
AND ab.is_billing = 1
billing_phone is the most recent addition ... this is of course not working and giving me:
1054 - Unknown column 'civicrm_contact.id' in 'where clause'
Due to the complexity, and the fact that there are more fields yet to add, I'd like to avoid JOINs if possible, and stay as close to the current syntax as possible.
When I tried to add billing phone with the same pattern as the others, it became apparent that not everyone has a billing phone - the number of rows dropped dramatically.
I'd like to make this work as a subquery in the outer SELECT (which would get me NULLs for those rows with no billing phone, right?).
Actually, it doesn't work as a JOIN either:
SELECT c.id, p.id, p.register_date, o.office_agency_name_1, a.street_address, a.supplemental_address_1, a.city, s.abbreviation, co.iso_code, a.postal_code, ph.phone, pf.phone, e.email,
ab.name, c.addressee_custom, pb.phone as billing_phone
FROM civicrm_contact c,
civicrm_participant p,
civicrm_value_office_info_1 o,
civicrm_address a,
civicrm_state_province s,
civicrm_country co,
civicrm_phone ph,
civicrm_phone pf,
civicrm_email e,
civicrm_address ab
LEFT JOIN civicrm_phone as pb on pb.contact_id = c.id AND pb.is_billing = 1
WHERE p.contact_id = c.id
AND p.is_test = 0
AND p.event_id = 1
AND p.status_id NOT IN (4,11,12)
AND ( c.is_deleted = 0 OR c.is_deleted IS NULL )
AND o.entity_id = c.id
AND a.contact_id = c.id
AND s.id = a.state_province_id
AND co.id = a.country_id
AND ph.contact_id = c.id
AND ph.is_primary = 1
AND pf.contact_id = c.id
AND pf.phone_type_id = 3
AND e.contact_id = c.id
AND e.is_primary = 1
AND ab.contact_id = c.id
AND ab.is_billing = 1
That gives me:
1054 - Unknown column 'c.id' in 'on clause'
How do I make this work?
What it appears is that the aliased table "c" (which refers to civicrm_contact) doesn't have a column "id" in it.
Are you sure this column exists in the table?
Related
I have the next query:
SELECT DISTINCT
bt.name, b.id
FROM
ports po,
cities c,
provinces p,
countries co,
states s,
translations t,
element_types et,
languages l,
boat_models bm,
boat_types bt,
boats b
JOIN
boat_prices bprf ON b.id = bprf.boat_id
AND bprf.checkin_date IS NULL
AND bprf.duration_id IS NULL
WHERE
t.element_translation = 'España'
AND et.name = 'Country'
AND s.name = 'confirmed'
AND s.id = b.state_id
AND l.locale = 'es'
AND t.language_id = l.id
AND t.element_type_id = et.id
AND t.element_id = p.country_id
AND c.province_id = p.id
AND po.city_id = c.id
AND b.port_id = po.id
AND bm.id = b.boat_model_id
AND bt.id = bm.boat_type_id
That is working perfectly and returning 9 rows:
'BOAT_TYPE_CATAMARAN','13707'
'BOAT_TYPE_SAILBOAT','13700'
'BOAT_TYPE_SAILBOAT','13701'
'BOAT_TYPE_SAILBOAT','13702'
'BOAT_TYPE_SAILBOAT','13703'
'BOAT_TYPE_SAILBOAT','13704'
'BOAT_TYPE_SAILBOAT','13705'
'BOAT_TYPE_SAILBOAT','13706'
'BOAT_TYPE_SAILBOAT','13708'
I want to group the results by boat type and get the number of boats per type.
However, when I do:
SELECT DISTINCT
bt.name, COUNT(b.id) AS num_boats
FROM
ports po,
cities c,
provinces p,
countries co,
states s,
translations t,
element_types et,
languages l,
boat_models bm,
boat_types bt,
boats b
JOIN
boat_prices bprf ON b.id = bprf.boat_id
AND bprf.checkin_date IS NULL
AND bprf.duration_id IS NULL
WHERE
t.element_translation = 'España'
AND et.name = 'Country'
AND s.name = 'confirmed'
AND s.id = b.state_id
AND l.locale = 'es'
AND t.language_id = l.id
AND t.element_type_id = et.id
AND t.element_id = p.country_id
AND c.province_id = p.id
AND po.city_id = c.id
AND b.port_id = po.id
AND bm.id = b.boat_model_id
AND bt.id = bm.boat_type_id
GROUP BY bt.name
ORDER BY bt.name
I´m getting:
'BOAT_TYPE_CATAMARAN','241'
'BOAT_TYPE_SAILBOAT','1928'
but according to the first query, I´m expecting
'BOAT_TYPE_CATAMARAN','1'
'BOAT_TYPE_SAILBOAT','8'
What am I missing?
I suspect that you want:
SELECT bt.name, COUNT(DISTINCT b.id) AS num_boats
FROM ...
WHERE ...
GROUP BY bt.name
ORDER BY bt.name
That is: move the DISTINCT within the COUNT() rather than directly in the SELECT.
Generally speaking, DISTINCT and GROUP BY do not go along well together; DISTINCT is already aggregation in essence, so mixing both is usually not relevant.
Note that your syntax uses old-school, implicit joins (with a comma in the FROM clause): you should be using standard joins (with the ON keyword), whose syntax has been state-of-the-art for decades.
You are doing a distinct in your first query so you are 'hiding' a lot if rows that gets doubled because of your join.
Using this query will get the result as in shown in below image link
select c.property_title,b.property_title as building,d.property_title as floor,e.house_name as house,
f.room_no as room,g.bed_no as bed
from g_property_group c
left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
left join g_room f on f.house_id = e.id and f.is_deleted = '0'
left join g_bed g on g.room_id = f.id and g.is_deleted = '0'
where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
and c.is_deleted = '0'
I have written the query for fetching the count of parent and child .will get the building count as 7 floor count as 7 house count as 5 room count as 4, instead the count of building should be 2, floor = 2, house = 4, Rooms = 5 and Bed 1.
select c.property_title,count(b.property_title)as building,count(d.property_title)as floor,count(e.house_name)as house,
count(f.room_no)as room,count(g.bed_no) as bed
from g_property_group c
left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
left join g_room f on f.house_id = e.id and f.is_deleted = '0'
left join g_bed g on g.house_id = g.id and g.room_id = f.id and g.is_deleted = '0'
where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
and c.is_deleted = '0'
Thank you in advance
Try this:
SQL SERVER
select count([building]) over (order by building) as [bulding count],
count([floor]) over (order by [floor],[building]) as [floor count],
count([house]) over (order by building,[floor],[house]) as [house count],
count([room]) over (order by building,[floor],[house],[room]) as [room count],
count([bed]) over (order by building,[floor],[house],[room],[bed]) as [room count]
I have the following query I am trying to join 2 tables (' Industry' , 'Country' ) on 2 conditions, but it gives me the following error
Error Code: 1054. Unknown column 'i.id' in 'on clause'
Does anybody know how should I tackle this?
SELECT c.name AS country_name, i.name as industry_name, num_projects, num_consultants, admin_rating
FROM industry i, country c
JOIN (SELECT pc.country_id, pi.industry_id, COUNT(p.id) AS num_projects
FROM project p, project_country pc, project_industry pi
where p.id = pc.project_id and pi.project_id=p.id
GROUP BY pc.country_id,pi.industry_id) x ON x.country_id = c.id and x.industry_id=i.id
JOIN (SELECT u.country_id,ie.industry_id, COUNT(u.id) AS num_consultants
FROM user u, consultant_profile, industry_experience ie
WHERE u.is_active = 1 AND u.type = 0 and
ie.consultant_profile_id= consultant_profile.id
and u.id= consultant_profile.id
GROUP BY u.country_id,ie.industry_id) y ON y.country_id = c.id and y.industry_id = i.id order by num_projects DESC limit 20;
EDIT the table structure is as following:
industry - id
project_industry - industry_id, project_id
industry_experience - consultant_profile_id, industry_id
consultant_profile - id,user_id
Since you still did not provide any sql fiddle
you can start from my one:
http://sqlfiddle.com/#!9/6c0569/1
SELECT pc.country_id, pi.industry_id,
COUNT(p.id) AS num_projects,
COUNT(u.id) AS num_consultants
FROM project p
INNER JOIN project_country pc
ON p.id = pc.project_id
INNER JOIN project_industry pi
ON pi.project_id=p.id
INNER JOIN `user` u
ON u.is_active = 1 AND u.type = 0
and u.country_id = pc.country_id
INNER JOIN industry_experience ie
ON u.id = ie.consultant_profile_id
AND ie.industry_id = pi.industry_id
GROUP BY pc.country_id, pi.industry_id
if you will add some data into that fiddle we can discuss deeper
I have 1 table with string type. (photo_add, photo_comment etc.)
Then I have 4 other tables that are connected with these types.
I need to make a query which will choose the data from all these tables according to the type.
I created this but it does not work.
SELECT DISTINCT a.id, a.event, a.params, a.created_at, a.item_id, a.sender_id, a.receiver_id, u.name, u.id as userid
FROM `eva49_lovefactory_activity` a, eva49_users u
CASE a.event
WHEN "photo_add" THEN
INNER JOIN eva49_lovefactory_photos lp ON lp.id = a.item_id AND lp.fsk18 = -1
WHEN "group_create" THEN
INNER JOIN eva49_lovefactory_groups lg ON lg.id = a.item_id AND lg.fsk18 = -1
WHEN "photo_comment" THEN
INNER JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND lic.fsk18 = -1
WHEN "profile_comment" THEN
INNER JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND lic.fsk18 = -1
WHEN "profile_comment" THEN
INNER JOIN eva49_lovefactory_profiles lp ON lp.user_id = a.receiver_id AND lp.status_fsk18 = -1
ELSE 1=1
END
WHERE (u.id = a.sender_id OR u.id = a.receiver_id) and (u.id <> 379)
ORDER BY a.created_at DESC LIMIT 0, 10
Is there any way how to pair the tables? The database structure was given to me like this.
EDIT:
There is a table "eva49_lovefactory_activity" which contains information about recent activity. It has a column "event" that contains string with the event name (photo_add, photo_comment etc.)
There are other tables - eva49_lovefactory_photos, eva49_lovefactory_profiles, eva49_lovefactory_groups etc. In these tables I have to find whether the item was approved by admin. If it was I can show it. (fsk18 = -1)
Move CASE condition to JOIN condition and do LEFT JOIN..
SELECT DISTINCT a.id, a.event, a.params, a.created_at, a.item_id, a.sender_id, a.receiver_id, u.name, u.id as userid
FROM `eva49_lovefactory_activity` a, eva49_users u
LEFT JOIN eva49_lovefactory_photos lp ON lp.id = a.item_id AND lp.fsk18 = -1
AND a.event='photo_add'
LEFT JOIN eva49_lovefactory_groups lg ON lg.id = a.item_id AND lg.fsk18 = -1
AND a.event='group_create'
LEFT JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND
lic.fsk18 = -1 AND a.event='photo_comment'
LEFT JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND
lic.fsk18 = -1 AND a.event='profile_comment'
WHERE (u.id = a.sender_id OR u.id = a.receiver_id) and (u.id <> 379)
ORDER BY a.created_at DESC LIMIT 0, 10
I have this query:
SELECT CONCAT(f.name, ' ', f.parent_names) AS FullName,
stts.name AS 'Status',
u.name AS Unit,
city.name AS City,
hus.mobile1 AS HusbandPhone,
wife.mobile1 AS WifePhone,
f.phone AS HomePhone,
f.contact_initiation_date AS InitDate,
fh.created_at AS StatusChangeDate,
cmt.created_at AS CommentDate,
cmt.comment AS LastComment,
f.reconnection_date AS ReconnectionDate,
(
SELECT GROUP_CONCAT(t.name)
FROM taggings tgs JOIN tags t
ON tgs.tag_id = t.id
WHERE tgs.taggable_type = 'family' AND
tgs.taggable_id = f.id
) AS HandlingStatus
FROM families f
JOIN categories stts ON f.family_status_cat_id = stts.id
JOIN units u ON f.unit_id = u.id
JOIN categories city ON f.main_city_cat_id = city.id
JOIN contacts hus ON f.husband_id = hus.id
JOIN contacts wife ON f.wife_id = wife.id
JOIN comments cmt ON f.id = cmt.commentable_id AND
cmt.created_at = (SELECT MAX(created_at) FROM comments WHERE commentable_id = f.id)
JOIN family_histories fh ON f.id = fh.family_id AND
fh.created_at = (SELECT MAX(created_at) FROM family_histories WHERE family_id = f.id AND family_history_cat_id = 1422) AND
fh.family_history_cat_id = 1422
WHERE f.id = 12212
The problem is with the second SELECT (the column - HandlingStatus).
I don't understand but when the column has results (tested as a stand alone query) - I get an empty result set, and when there are no results - I get a result.
Why?