codeigniter join not working - codeigniter

<?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.*')

Related

how to convert query this query to laravel

SELECT *
FROM `digital_useraccess`
left join digital_publisher ON
access_publisher = pub_auto
left join digital_issue ON
issue_publisher = pub_auto
WHERE 1
without using laravel DB class method
Try this as reference
ModelName::where(conditions)
->leftjoin('digital_publisher','access_publisher','pub_auto')
->leftjoin('digital_issue','issue_publisher ','pub_auto')
->select('column_1','column_2',...)->get();
This might help :
::query()->where('<cplumn>' , '<operator>' ,
'<value>')->leftJoin('digital_publisher', 'access_publisher',
'=','pub_auto')->leftJoin('digital_issue', 'issue_publisher', '=',
'pub_auto')->select('<columns>')->get();/find();/first();

Active records class in CodeIgniter

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);

Multiple condition in Join Magento

Can Anyone tell me?
How can i write this type of query in magento
(Multiple condition in on)
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON (Customers.CustomerID=Orders.CustomerID and Orders.status = 1)
//I know this type
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key', array('table_alias.*'));
How can i add multiple condition in JOIN ?
I found the answer myself, It turnout to be very easy
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key and table_alias.columnname = ".."' , array('table_alias.*'));

Doctrine query only returning one row?

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)

Codeigniter: Select from multiple tables

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();

Resources