2

I'm trying to do a subquery with

$this->db->where(" EXISTS (SELECT * FROM myTable)");

But it doesnt work, the output of this is: myquery + WHERE 'EXISTS (SELECT * FROM myTable);

That quote before the EXISTS makes the query unresolvable!

Does anyone knows how to solve it?

1
  • 2
    this might shed some light Commented Aug 29, 2014 at 3:38

4 Answers 4

6

please remove space before and after EXISTS keyword.that does not display any error.

$this->db->where("EXISTS(SELECT * FROM myTable)");
Sign up to request clarification or add additional context in comments.

Comments

4

Maybe you could try to set the escape to false by using

$this->db->where(" EXISTS (SELECT * FROM myTable)", null, false);

This is the snippet of where() in DB_active_rec.php

public function where($key, $value = NULL, $escape = TRUE)

Comments

1

Just try this.

Instead of using 'where' clause, please write down the complete query string & execute the query using $this->db->query();

    $qry_string= $yourquery . "WHERE EXISTS (SELECT * FROM myTable)";
    $this->db->query($qry_string);

Comments

0

To avoid writing raw SQL and to ensure escaping/quoting in your subquery, use get_compiled_select() to render the subquery. I assume that you'll have more logic in your subquery, but for the asked question you can pass the table name into get_compiled_select() and SELECT * will be the default SELECT clause applied.

Once the subquery is safely rendered, you just turn off the escaping in its parent WHERE clause by passing false as the third parameter of where().

$this->db->where('EXISTS (' . $this->db->get_compiled_select('myTable') . ')', null, false);

This WHERE clause of your parent query will be rendered as:

WHERE EXISTS (SELECT * FROM `myTable`)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.