Basicly i have two tables photos and users. I wanna join tables and Update colums image_max and image_min. I get error unknown colum username. In which way i can join two tabels and get data from both. My sintax is:
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
And I get error
Unknown column username in where clause
UPDATE `photos` SET `image_max` = '', `image_min` = '' WHERE `username` = 'wwww'
apparently you need a letter on the table should say "users.username", check that.
Greetings.
$this->db->select('*');
$this->db->from('photos');
$this->db->join('users', 'photos.id = users.id');
$this->db->where('users.username',$username);
$this->db->update('photos',$data);
You don't need to use "select and from" before upload fields, just update in this way
$data = array('image_max'=> 4, 'image_min' => 1);
$this->db->join('users', 'photos.id = users.id');
$this->db->where('username',$username);
$this->db->update('photos',$data);
Related
I have 4 tables like this:
Now, I want to select all columns from all tables where model.featured=1. i.e.
model.id
model.name
model_attributes.id
model_attributes.attributes_value
model_images.id
model_images.model_images
attributes.id
attributes.name
attributes.value
I can only do basic level queries and I'm not sure if I'm anywhere near to the solution but this is what I have tried (returns nothing):
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model.id = model_images.model_id','RIGHT');
$this->db->join('model_attributes','model.id = model_attributes.model_id','RIGHT');
$this->db->join('attributes','model_attributes.attributes_id = attributes.id','RIGHT');
$this->db->where('model.featured', 1);
$query = $this->db->get();
return $query->result();
How do I achieve what I want ? Or, is there any other better methods to do it ?
// try this query
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model_images.model_id = model.id');
$this->db->join('model_attributes','model_attributes.model_id = model.id');
$this->db->join('attributes','attributes.id = model_attributes.attributes_id');
$this->db->where('model.featured','1');
$query = $this->db->get()->result();
return $query;
Please go through below mentioned solution.
$this->db->select('model.*,model_images.*,model_attributtes.*, attributes.*'):
Let me know if it not works for you.
Please go through below solution. If multiple table has same column name then you have to give alias for each column name which have same name in both the table. because by default CI merge all column name and generate result.
$this->db->select('*,c.name as c_name,s.name as s_name');
$this->db->from('country c');
$this->db->join('state s', 'c.id = s.country_id');
$query = $this->db->get();
can I convert this query
"SELECT * FROM (SELECT * FROM blog_post ORDER BY data DESC) blog_post GROUP BY blog_id LIMIT 2"
into a Yii2 active record query?
Thx
Ms
Yes, you can do this.
Yii2 gave us a wonderful library support.
You can form custom sql query and pass this query in findBySql() like:
$sql = "Some query/nested query";
$result = ModelClass::findBySql($sql);
Visit Yii official documentation.
BlogPost::find()
->orderBy('data DESC')
->groupBy('blog_id')
->limit(2)
->all();
I suppose you can do in this way :
Ⅰ:create a subQuery where you select from.
$blogPostQuery = BlogPostModel::find()->orderBy(['data' => SORT_DESC]);
Ⅱ:Get activeRecord results. The from params is an exist Query.
$models = (new yii\db\Query)
->from(['blog_post ' => $blogPostQuery])
->groupBy(['blog_id'])
->limit(2)
->all();
ps:
see yii\db\query->from()-detail in Yii Api;
(public $this from ( $tables )) $tables can be either a string (e.g. 'user') or an array (e.g. ['user', 'profile']) specifying one or several table names··· or a sub-query or DB expression
Have a try!I hope it`s useful for you:))
<?php
$this->db->select('*');
$this->db->from('venue');
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
$this->db->join('venue_type vt2', 'vt2.venue_type_id = venue.venue_type_id2');
$this->db->join('venue_subtype vst2', 'vst2.venue_subtype_id = venue.venue_subtype_id2');
$this->db->join('venue_type vt3', 'vt3.venue_type_id = venue.venue_type_id3');
$this->db->join('venue_subtype vst3', 'vst3.venue_subtype_id = venue.venue_subtype_id3');
$this->db->where('venue_id',$id);
$query = $this->db->get();
i have venue table it has more then 1 field relation b/w venue_type. When i try to give first relation
<?php
$this->db->join('venue_type vt1', 'vt1.venue_type_id = venue.venue_type_id1');
$this->db->join('venue_subtype vst1', 'vst1.venue_subtype_id = venue.venue_subtype_id1');
its working fine , but i try to access whole its not working.
Please Help me. (It may simple but i stuck)
By Saravanan.
You need to use alias for multiple joins.
SELECT st_id, table1.us_login, table2.us_login
FROM (stream)
LEFT JOIN users AS table1 ON table1.us_id = stream.st_id_user_from
LEFT JOIN users AS table2 ON table2.us_id = stream.st_id_user_to
see the link: http://codeigniter.com/forums/viewthread/151176/
There's no $this->db->from function in Codeigniter. Use instead $this->db->select('venue.*')
I'm new to Doctrine but somewhat familiar with SQL. I have a very simple schema with Users and Challenges. Each Challenge has a "challenger id" and a "opponent id" which are foreign keys into the User table. I want to print a list of all challenges, with the output being the names from the User table. Here is my Doctrine query;
$q = Doctrine_Query::create()
->select('u1.name challenger, u2.name opponent')
->from('Challenge c')
->leftJoin('c.Challenger u1')
->leftJoin('c.Opponent u2');
The problem is that this only returns one row. I've used the getSqlQuery() command to look at the generated SQL which ends up being:
SELECT u.name AS u__0, u2.name AS u2__1 FROM challenge c
LEFT JOIN user u ON c.challenger_id = u.id
LEFT JOIN user u2 ON c.opponent_id = u2.id
When run in a 3rd party SQL client this query retrieves all of the rows as expected. Any idea how I can get all of the rows from Doctrine? I'm using $q->execute() which I understand should work for multiple rows.
Thanks.
For me it worked by chaning the hydration mode:
$result = $query->execute(array(), Doctrine_Core::HYDRATE_SCALAR);
Set result set then returns an array instead of objects.
I just ran into this issue and in my case the problem was that my query didn't select any field from the FROM table. Example:
$query = Doctrine_Query::create()
->select(
'ghl.id as id,
ghl.patbase_id as patbase_id,
ghl.publication_no as publication_no,
ghl.priority_no as priority_no
'
)
->from('GridHitListContents ghlc')
->leftJoin('ghlc.GridHitList ghl')
As you can see there is no selected field from the GridHitListContents table.
with a $query->count() I got 2000ish results, but with $query->fetchArray() only the first one.
When I added
$query = Doctrine_Query::create()
->select(
'ghlc.id,
ghl.id as id,
...
'
)
->from('GridHitListContents ghlc')
->leftJoin('ghlc.GridHitList ghl')
I got back all my results.
$query->fetchOne() work fine for me.
Use this $result = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY)
How can I select rows from two or more tables?
I'm setting default fields for a form, and I need values from two tables...
My current code reads:
$this->CI->db->select('*');
$this->CI->db->from('user_profiles');
$this->CI->db->where('user_id' , $id);
$user = $this->CI->db->get();
$user = $user->row_array();
$this->CI->validation->set_default_value($user);
The example in the User Guide should explain this:
$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id
See the whole thing under Active Record page in the User Guide.
Just add the other table to the "->from()" method. Something like:
$this->db->select('t1.field, t2.field2')
->from('table1 AS t1, table2 AS t2')
->where('t1.id = t2.table1_id')
->where('t1.user_id', $user_id);
I think the question was not so much about joins as how to display values from two different tables - the User Guide doesn't seem to explain this.
Here's my take:
$this->db->select('u.*, c.company, r.description');
$this->db->from('users u, company c, roles r');
$this->db->where('c.id = u.id_company');
$this->db->where('r.permissions = u.permissions');
$query = $this->db->get();
I think the syntax is incorrect.
You need to select one record. I have two tables, and I have an id from one table transfer by parameter, and the relation of both tables.
Try this
$this->db->select('*')
->from('student')
->where('student.roll_no',$id)
->join('student_details','student_details.roll_no = student.roll_no')
->join('course_details','course_details.roll_no = student.roll_no');
$query = $this->db->get();
return $query->row_array();
$SqlInfo="select a.name, b.data fromtable1 a, table2 b where a.id=b.a_id";
$query = $this->db->query($SqlInfo);
try this way, you can add a third table named as c and add an 'and' command to the sql command.
// Select From Table 1 All Fields, and From Table 2 one Field or more ....
$this->db->select('table1.*, table2.name');
$this->db->from('table1, table2');
$this->db->where('table2.category_id = table1.id');
$this->db->where('table2.lang_id',$id); // your where with variable
$query = $this->db->get();
return $query->result();