Yii Framework Forum: How To Write This Select In Query In Query Builder - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Write This Select In Query In Query Builder Rate Topic: -----

#1 User is offline   Jonny 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 204
  • Joined: 26-December 11

Posted 16 June 2013 - 05:45 PM

Hi,

I'm trying to execute this query in querybuilder.

SELECT id, title, area_id From item WHERE item.area_id IN (SELECT item.area_id FROM item WHERE item.id = 3)


I've read the docs for the Where() In operator but i'm still having some trouble getting it to execute. What would be the correct way?

Thanks

Jonny
0

#2 User is offline   kumarkulandai 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 18-November 11
  • Location:Bangalore

Posted 16 June 2013 - 10:27 PM

instead of this

SELECT id, title, area_id From item WHERE item.area_id IN (SELECT item.area_id FROM item WHERE item.id = 3)

can you try this?

SELECT
id, title, area_id
FROM
item a
INNER JOIN item b
ON a.area_id =b.id
WHERE
b.id = 3
Regards,
Kumar Kulandai.

Posted Image
0

#3 User is offline   Jonny 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 204
  • Joined: 26-December 11

Posted 16 June 2013 - 10:45 PM

Thanks for the reply, yours worked in my SQL client. But I seem to be having a real problem with backticks (I think).


CDbCommand::() failed: SQLSTATE[42000]: Syntax error or access violation:
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
'.`title, i`.`area_id, i`.`slug`...


SELECT `i`.`id,` AS `i`.`title, i`.`area_id, a`.`slug`
FROM `item` `i`
JOIN `item` `b` ON i.area_id = b.id
WHERE i.id=:id.

Is how it was being executed.

I had the same outcome when trying to use this method http://www.yiiframew...-and-condition/
0

#4 User is offline   Jonny 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 204
  • Joined: 26-December 11

Posted 16 June 2013 - 11:10 PM

I removed the $cmd->select as an array and just quoted it like:

$cmd->select = 'i.id, i.title, i.slug';

Hope that help anyone else.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users