Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Basic Join Not Working (Copy From LAMP Forum)

Basic Join Not Working (Copy From LAMP Forum)

Scheduled Pinned Locked Moved Database
questionlampdatabasecode-review
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    Hello there. I am trying to get data from 3 different tables based on simple join. One of the tables can have multiple values against one primary key. Here are the table designs Table 1 - EmployeeDetails

    EmployeeId INT, FirstName VARCHAR, SurName VARCHAR, SexId INT

    Table 2 - EmployeeSex

    SexId INT, Sex VARCHAR
    -- Values inserted
    -- 1, Male
    -- 2, Female

    Table 3 - EmployeeContacts

    EmployeeId INT, Contact VARCHAR -- may or may not be null AND can contain multiple values

    I am using following query

    SELECT ED.EmployeeId, ED.FirstName, ED.SurName, GROUP_CONCAT(EC.Contact)
    FROM EmployeeDetails ED, EmployeeSex ES, EmployeeContacts EC
    WHERE ED.SexId = ES.SexId AND ED.EmployeeId = EC.EmployeeId AND ED.EmployeeId = 'emp_password';

    Now this query works fine if we have at least one contact number. But if there are not contacts, then this results in empty set. What is wrong with this query ? How can I improve so that it works in all scenarios (regardless of number of contacts in EmployeeContacts table). Thanks for any input.

    C 1 Reply Last reply
    0
    • D Django_Untaken

      Hello there. I am trying to get data from 3 different tables based on simple join. One of the tables can have multiple values against one primary key. Here are the table designs Table 1 - EmployeeDetails

      EmployeeId INT, FirstName VARCHAR, SurName VARCHAR, SexId INT

      Table 2 - EmployeeSex

      SexId INT, Sex VARCHAR
      -- Values inserted
      -- 1, Male
      -- 2, Female

      Table 3 - EmployeeContacts

      EmployeeId INT, Contact VARCHAR -- may or may not be null AND can contain multiple values

      I am using following query

      SELECT ED.EmployeeId, ED.FirstName, ED.SurName, GROUP_CONCAT(EC.Contact)
      FROM EmployeeDetails ED, EmployeeSex ES, EmployeeContacts EC
      WHERE ED.SexId = ES.SexId AND ED.EmployeeId = EC.EmployeeId AND ED.EmployeeId = 'emp_password';

      Now this query works fine if we have at least one contact number. But if there are not contacts, then this results in empty set. What is wrong with this query ? How can I improve so that it works in all scenarios (regardless of number of contacts in EmployeeContacts table). Thanks for any input.

      C Offline
      C Offline
      Chris Quinn
      wrote on last edited by
      #2

      Use joins, not a where condition

      SELECT ED.EmployeeId, ED.FirstName, ED.SurName, GROUP_CONCAT(EC.Contact)
      FROM EmployeeDetails ED
      INNER JOIN EmployeeSex ES on ED.SexId = ES.SexId
      LEFT JOIN EmployeeContacts EC on ED.EmployeeId = EC.EmployeeId
      WHERE ED.EmployeeId = 'emp_password';

      If there are instance where an employee does not have an assigned sex, change the INNER JOIN to a LEFT JOIN

      ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups