CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • SQL issue - same entry returned twice
Pages: [1]

Author Topic: SQL issue - same entry returned twice  (Read 893 times)

kengle3

  • I’m new here
  • *
  • Posts: 16
  • Karma: 0
SQL issue - same entry returned twice
March 27, 2010, 05:26:02 pm
Right now I'm trying to write my own function to query information required from the civicrm database for an AJAX call since the civicrm API functions for returning data are too inefficient for my needs. However I'm getting certain rows reported more than once when I pull information from more than two tables. Probably because I'm not very good with SQL.

Example query with 2 tables:
SELECT civicrm_contact.first_name AS first, civicrm_contact.last_name AS last, civicrm_participant.id AS id FROM civicrm_participant, civicrm_contact WHERE civicrm_participant.contact_id=civicrm_contact.id

This is returning (with output format being first - last - id)
Mister - Johnson - 2139
Mister - Ross - 2140
Mister - Van Meter - 2141

However with 3 tables (in this case also trying to pull their street address)

SELECT civicrm_contact.first_name AS first, civicrm_contact.last_name AS last, civicrm_participant.id AS id, civicrm_address.street_address AS sreet FROM civicrm_participant, civicrm_contact, civicrm_address WHERE civicrm_participant.contact_id=civicrm_contact.id AND civicrm_address.contact_id=civicrm_participant.contact_id

This returns
Mister - Johnson - 2139
Mister - Johnson - 2139
Mister - Ross - 2140
Mister - Ross - 2140
Mister - Van Meter - 2141
Mister - Van Meter - 2141


Does anyone have any idea why this may be?

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: SQL issue - same entry returned twice
March 27, 2010, 06:58:01 pm
Anything to do with how many locations do your contacts have?
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

kengle3

  • I’m new here
  • *
  • Posts: 16
  • Karma: 0
Re: SQL issue - same entry returned twice
March 27, 2010, 07:28:54 pm
This time I'm outputting what I'm getting with the three table example except with the format first, last, id, street if not null. It doesn't seem to be consistent.

Mister Johnson 2139
Mister Johnson 2139
Mister Ross 2140 722 S. Apple Trail
Mister Ross 2140
Mister Van Meter 2141 911 Example St. Apt C
Mister Van Meter 2141

Looking further down the line, no one in this database has multiple addresses. Just one address listed or null. The address always only shows up in the first listing.

This means that when my query is parsed, its not joining my results together right? I'm not sure what to do since I thought that by confirming that id's from all tables matched it would ensure I'd only get one table with unique rows.
« Last Edit: March 27, 2010, 07:40:29 pm by kengle3 »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • SQL issue - same entry returned twice

This forum was archived on 2017-11-26.