Need help for mySQL query for my forum dashboard - php

My forum Dashboard
I got 2 tables forum_category and forum_topics. the problem is that the
the latest post is not accurate... I don't know the query for it.
Can anyone help me ?
The sample output is on the image ... I'm using codeigniter for that
The category contains columns id,name,desc
topic contains of columns of id,subject,desc,date_posted and posted_by
Query:
$select = array(
'forum_category.f_cat_name',
'forum_category.f_cat_id',
'forum_category.f_cat_desc',
'forum_topic.topic_subject as last_posted',
'max(forum_topic.date_posted) as last_date_posted',
'max(forum_topic.posted_by) as last_posted_by',
'count(forum_topic.topic_id) as total'
);
$query=$this->db
->select($select)
->from('forum_category')
->where('f_cat_type',1)
->where('f_cat_status',1)
->join('forum_topic','forum_topic.f_cat_id = forum_category.f_cat_id','inner')
->group_by('forum_category.f_cat_id')
->get();
return $query->result();

Join clause should come before where clause.
try this,
$query=$this->db
->select($select)
->from('forum_category')
->join('forum_topic','forum_topic.f_cat_id = forum_category.f_cat_id','inner')
->where('f_cat_type',1)
->where('f_cat_status',1)
->group_by('forum_category.f_cat_id')
->get();
return $query->result();

Related

Codeigniter : Join 2 tables based on where condition

I have to select data from two tables for APIs.
There are two tables garage_services, and garages. I receive garage_sevices id.
Garage_services table is like this
id | garage_id | service _id
Now from this table I select row based on service_id from here I have to select garage_id and get details about garage from garage table
$garages = $this->db
->select('*')
->from('garage_services')
->join('garage', 'garage_services.id=garage.id')
->where($where)
->get();
Above is the query I came up with, I don't know if its correct. as I am not sure about $where as well.
Please help me with this
Here I have written the query for your solution.
$garages = $this->db->from('garage_services as gs, garages as g')
->where('g.id', $garage_service_id, FALSE)
->get();
Here $garage_service_id is the variable that passed in function argument as you have written you in your question.
Try this
->select('*')
->from('garage_services as t1')
->join('garage as t2', 't1.garage_id = t2.id')
->where('t1.id', $service _id)
->get();
As per the detail is given in the question your query look correct and for the where condition:
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.col1 = table2.col1');
$this->db->where('table1.col1', 2);
$query = $this->db->get();

Codeigniter SQL query no records in other table

I would like to join 2 tables but there is no record in the other table.
For example
Table name: shop_items
shop_item_id
shop_item_sk
shop_item_name
Table name: item_reviews
item_review_id
item_review_sk
item_review_desc
item_review_rate
shop_item_sk
item_review_by
The problem is that it the item details doesn't appear when it don't have any record in the item_reviews table
this is my SQL statement
$query = $this->db->select('*')
->from('shop_items')
->join('item_reviews', 'shop_items.shop_item_sk=item_reviews.shop_item_sk','inner')
->where('shop_items.shop_item_sk',$id)
->limit(1)
->get();
return $query->result();
I researched about UNION but I don't know how to use it in Codeigniter.
Advance thank you for those who can help me.
Use left outer join. Here shop_items table is the left table join item_reviews with it.
Maybe with a left join ?
$query = $this->db->select('*')
->from('shop_items')
->join('item_reviews', 'shop_items.shop_item_sk=item_reviews.shop_item_sk','left')
->where('shop_items.shop_item_sk',$id)
->limit(1)
->get();
return $query->result();

Query for getting values from multiple tables

I have a table video with fields videoid, genre(int-foreign key), language(int-foreign key) etc. I want to get the value of genre from genre table and language from movie_languages table.
The structure of tables are given below:
video
genre
movie_languages
How can I join these 3 tables to get the language and genre related to each videos in video table. Also when user didn't select genre/language in the form, value 0 will be inserted to the table. Will this affect the query. I am using codeingiter and I tried with the following query and is not working.
$this->db->select('video.*,movie_languages.language,genre.genre');
$this->db->join('genre', 'video.genre = genre.id');
$this->db->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();
Please help me.
Thanks in advance.
It will be, you need a left join in this case
Try this
$query = $this->db
->select('v.*,ml.language,g.genre')
->from('video as v')
->join('movie_languages AS ml', 'v.language = ml.id', 'left outer')
->join('genre AS g', 'v.genre = g.id', 'left outer')
->get();
try this
$this->db->select('video.*,movie_languages.language,genre.genre')
->from('video')
->join('genre', 'video.genre = genre.id')
->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();

Codeigniter Active Record / MySQL Join Query - How to Return Results if one of the Table Rows is Not Present

I have the following query:
$this->db
->select('SQL_CALC_FOUND_ROWS null as rows
,table1.*
,table2.*
,table3.*', FALSE)
->from('table1')
->where('table1.column1', $user_id)
->join('table2', 'table2.column2 = table1.column2')
->join('table3', 'table3.column2 = table1.column2')
->group_by('table1.column2')
->order_by('table1.column2', 'DESC');
$query = $this->db->get();
The problem is, there may not be a row in table 3 and if there is not, I would still like to return a result with the remainder of the query data. Please could someone advise how to achieve this?
you should do a left join on table3
Use left join and also use group_by to get exact records:
$this->db->join('login_ranknames AS t4', 't4.id = t1.rank', 'left');
$this->db->group_by('t4.id');

codeigniter join 2 table data

hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work
$this->db->join();
what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.
i have tried to code a function using this example:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
EDIT:
instead of using join method in codeigniter is it possible to use a simple function to retrieve the two table data separately? all i want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately ?
It doesn't matter what table is first... Simply:
<?php
$this->db->select('t1.name, t2.something, t3.another')
->from('table1 as t1')
->where('t1.id', $id)
->join('table2 as t2', 't1.id = t2.id', 'LEFT')
->join('table3 as t3', 't1.id = t3.id', 'LEFT')
->get();
?>
here is how it works:
suppose we have two tables namely student, library.
note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_id column
Write the the select query as follows, in the brackets write what are all the things you want
note:write as shown below don't put quotes to each single one put it on whole at once.
*note: suppose we want name, phone_no. from student table and book_name form library table.*
$this->db->select('name, phone_number, book_name');
Now write the from query and write one of the table name(No rule)
$this->db->from('student');
Now join this with the another table with join query
$this->db->join('library', 'students.std_id=library_std_id');
Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)
$this->db->where('std_id', 1);
$q= $this->db->get();
That's it it's done now you can print and check the result.
$this->db->join('comments', 'comments.id = blogs.id');
With this line you tell: search me inside comments all comments with id equal blogs.id.
Usually is something like that I think:
$this->db->join('comments', 'comments.blogs_id = blogs.id');
You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments.
Isn't important the position of first or second value
Hi this will work for joining two tables in codeIgnator.
$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
$this->db->from('chat');
$this->db->join('post', 'chat.id = post.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}
You can change to the query as you like do trail and error method to get your appropriate results.
This is my code for joint many tables as possible.
I have 3 tables professeurs,publications and support.
public function toutesdonnées(){
$this->db->select("*");
$this->db->from('publication');
$this->db->join('support', 'publication.idsup = support.idsup');
$this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result();
}
else
{
return false;
}

Categories