0

I have to fetch values from two tables, I have wrote separate query for fetching from both tables. and both queries worked properly. but I need to get this in one result object. so that I have joined queries using UNION operator. but it throws some error. the query is given below

       $query1 = "SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id,dev_family.house_name,dev_ib_account_registration.account_id FROM (dev_members) 
            JOIN dev_family ON dev_family.id=dev_members.family_id
            JOIN dev_ib_account_registration ON dev_ib_account_registration.member_id=dev_members.id 

            UNION 

            SELECT dev_members.name,dev_members.id,dev_members.age,dev_members.family_id, dev_family.
            house_name,dev_ib_sub_member_registration.account_id FROM (dev_members) 
            JOIN dev_family  ON dev_family.id=dev_members.family_id
            JOIN dev_ib_sub_member_registration ON dev_ib_sub_member_registration.member_id=dev_members.id";

            $result = $this->db->query($query);
             return $result->result();

and the error is:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN dev_family ON dev_family.id=dev_members.family_id JOIN dev_ib_s' at line 8

5
  • Have you tried wrapping each select statement in parentheses? Commented Apr 15, 2014 at 6:10
  • When you debug the query with codeigniter what's been sent to Mysql? Have you checked if it's the same as you wrote? Commented Apr 15, 2014 at 6:10
  • @ChrisMcKnight yes , but not working Commented Apr 15, 2014 at 6:13
  • @medina yea , I have checked the same query, and error shows Commented Apr 15, 2014 at 6:14
  • You have declared $query1, but executed $query. Typo. Commented Sep 12 at 1:40

1 Answer 1

2

There is something wired with the usage of brackets in your second query

(dev_members) <---

when i use your query without them in Fiddle query works ,but using with brackets it produces syntax error ,so try them without ()

updated query

SELECT 
  dev_members.name,
  dev_members.id,
  dev_members.age,
  dev_members.family_id,
  dev_family.house_name,
  dev_ib_account_registration.account_id 
FROM
  dev_members
  JOIN dev_family 
    ON dev_family.id = dev_members.family_id 
  JOIN dev_ib_account_registration 
    ON dev_ib_account_registration.member_id = dev_members.id 
UNION
SELECT 
  dev_members.name,
  dev_members.id,
  dev_members.age,
  dev_members.family_id,
  dev_family.house_name,
  dev_ib_sub_member_registration.account_id 
FROM
  dev_members  
  JOIN dev_family 
    ON dev_family.id = dev_members.family_id 
  JOIN dev_ib_sub_member_registration 
    ON dev_ib_sub_member_registration.member_id = dev_members.id 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.