0

I am having a problem with where_in . I am trying to get the shop name which possess the lookbook had the specific point id

$this->db->select('shop');
$this->db->from('shopify_lookbook');
$this->db->where_in(
    'lookbook_id',
    'SELECT lookbook_id
     FROM shopify_point
     WHERE point_id = $pointid'
);

The problem is the query it generate

SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN('SELECT lookbook_id FROM shopify_point WHERE point_id = 543') 

It will give blank but when I try in mysql without '' in IN() like below

SELECT `shop`
FROM `shopify_lookbook`
WHERE `lookbook_id` IN(
    SELECT lookbook_id
    FROM shopify_point
    WHERE point_id = 543
)

It returns the shop name that I want. How can I erase '' in $this->db->where_in()

4
  • you dont need to use IN when using where_in $this->db->where_in('lookbook_id', $pointid); Commented Jan 30, 2020 at 10:20
  • $pointid and lookbook_id is different Commented Jan 30, 2020 at 10:23
  • Use your variables I just gave an example Commented Jan 30, 2020 at 10:31
  • 1
    Food for thought regarding NOT using a subquery inside of WHERE IN(): CodeIgniter SELECT query to return qualifying rows with their highest value in a specific column Commented Jun 3 at 6:24

2 Answers 2

1

You might use where instead and to construct your IN clause there:

$this->db->where('lookbook_id IN (SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid)', NULL, FALSE);
Sign up to request clarification or add additional context in comments.

Comments

0

Best practice recommends safely rendering the subquery, then injecting that safe subquery into the parent query.

$sub = $this->db
    ->select('lookbook_id')
    ->where('point_id', $pointid)
    ->get_compiled_select('shopify_point');

return $this->db
    ->select('shop')
    ->where_in('lookbook_id', $sub, false)
    ->get('shopify_lookbook')
    ->result_array();

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.