вопрос о sql запросах

11 views
Skip to first unread message

yuriy

unread,
May 30, 2010, 12:39:06 PM5/30/10
to СodeIgniter-ru
добрый день

вот это запрос нормьно работает

SELECT users.id, users.username, users.gender, users.bdate,
photos.name as foto, cities.name as city, countries.name_en as country
FROM (users) LEFT JOIN photos ON photos.user_id = users.id LEFT JOIN
cities ON cities.id = users.city_id LEFT JOIN countries ON
countries.id = cities.country_id
WHERE `users`.`id` <> 1 AND `users`.`gender` = 1 AND
cities.country_id=1012


function get_users($where = array(),$limit = FALSE, $offset = FALSE)
{
$this->db->select($this->table.'.id,'.$this->table.'.username,'.
$this->table.'.gender,'.$this->table.'.bdate');

$this->db->select($this->photos_table.'.name as foto' );
$this->db->join($this->photos_table, $this->photos_table.'.user_id =
'.$this->table.'.id', 'left');
$this->db->select('cities.name as city');
$this->db->join('cities', 'cities.id = users.city_id', 'left');
$this->db->select('countries.name_en as country');
$this->db->join('countries', 'countries.id = cities.country_id',
'left');
foreach ($where as $v){$this->db->where($v);}
$this->db->group_by("users.id");


if ($limit !== FALSE)
{
$this->db->limit($limit);
}
if ($offset !== FALSE)
{
$this->db->offset($offset);
}
$query = $this->db->get($this->table);
echo $this->db->last_query();
return $query->result();
}


при таком вызове

$where = array("users.id <> 1 ","users.gender = $gender");
$this->users_model->get_users($where , $this->pagination->per_page,
$page);

всё работает

но как только я добавляю

$where = array("users.id <> 1 ","users.gender =
$gender","cities.country_id = $country_id");
что должно быть эквиваленто верхнему sql запросу
то выходит ощибка


Unknown column 'cities.country_id' in 'where clause'

SELECT id FROM (users) WHERE `users`.`id` <> 1 AND `users`.`gender` =
1 AND `cities`.`country_id` = 1012

подскажите что я делаю неправильно ?




royals...@gmail.com

unread,
May 31, 2010, 1:13:32 AM5/31/10
to СodeIgniter-ru
`cities`.`country_id` - тут ошибка. В запросе не участвует таблица
cities.
Или тут она вообще не нужна или ее надо джойнить

yuriy

unread,
May 31, 2010, 12:50:35 PM5/31/10
to СodeIgniter-ru
как же ?

а $this->db->join('cities', 'cities.id = users.city_id', 'left');

On 31 Mai, 07:13, "royalstyle...@gmail.com" <royalstyle...@gmail.com>
wrote:

royals...@gmail.com

unread,
Jun 1, 2010, 4:05:33 AM6/1/10
to СodeIgniter-ru
все, что после этой строки: $this->db->select($this->table.'.id,'.

$this->table.'.username,'.
$this->table.'.gender,'.$this->table.'.bdate');

+ нужно добавить, что с чем джойнить(перед каждым джойном): $this->db-
>from(таблица);

Reply all
Reply to author
Forward
0 new messages