Is it possible to use VIEW table on JOIN statement? - mysql

Good Day Folks,
I have a children_tb and I JOIN residents_tbl to it. Then, on residents_tbl I created a view named family_head.
Is it possible to use family_head on another JOIN statement to children_tbl query statement?
Current query
$query = $this->db->select('*');
$query = $this->db->from('children_tbl');
$query = $this->db->join('residents_tbl', 'residents_tbl.residentID=children_tbl.childName', 'full');
---> ***I want to add another join here for view (family_head)***
$query = $this->db->where('childName', $id);
$query = $this->db->order_by('childID', 'DESC');
$query = $this->db->get('');
I try to use
$query = $this->db->join('family_head', 'family_head.residentID=children_tbl.familyHeadID', 'full');
but I got an error message:
Table 'db_name.family_head' doesn't exist
TIA,
C.M.

This issue was already solved.
table names are just mismatched.

Related

Is there a better way to join query in laravel eloquent.?

I have two tables and both have separate models from them. first table is users and other table is users_details table I am writing an eloquent query to join the result but I am getting following error
Integrity constraint violation: 1052 Column 'gender' in where clause is ambiguous (SQL: select count(*) as aggregate from `users_details` right join `users` on `users_details`.`user_id` = `users`.`id` where `gender` LIKE female and `matrimonial` LIKE 1)
my code for the query is following.
public function bride(){
$query = app(UserDetail::class)->newQuery();
$query= $query->where('gender','LIKE','female');
$query= $query->where('matrimonial','LIKE','1');
$query = $query->rightJoin('users','users_details.user_id','=','users.id');
$request = request();
if(request()->exists('sort')){
$sorts = explode(',',request()->sort);
foreach ($sorts as $sort){
list($sortCol, $sortDir) = explode('|',$sort);
$query = $query->orderBy($sortCol,$sortDir);
}
}
else {
$query = $query->orderBy('id','asc');
}
if($request->exists('filter')) {
$query->where(function($q) use($request){
$value = "%{$request->filter}%";
$q->where('name','like',$value)
->orWhere('father_name','like',$value)
->orWhere('city','like',$value)
->orWhere('mother_name','like',$value);
});
}
$per_page = request()->has('per_page')?(int) request()->per_page : null;
$pagination = $query->paginate($per_page);
$pagination->appends([
'sort'=>request()->sort,
'filter'=>request()->filter,
'per_page'=>request()->per_page
]);
return response()->json(
$pagination
)
->header('Access-Control-Allow-Origin','*')
->header('Access-Control-Allow-Methods','GET');
}
What should I do to get the result. Thanks
Because there are same column name in your tables(gender in user_details and users), you need to specify the table name with the column, so that mysql can find the true column for you,
like this:
$query= $query->where('user_details.gender','LIKE','female');
SQL doesn't know which table you are talking about when you are using Joins - unless you specify table names for each column mentioned in your query.
Replace these lines:
$query= $query->where('gender','LIKE','female');
$query= $query->where('matrimonial','LIKE','1');
with these lines:
$query= $query->where('users_details.gender','LIKE','female');
$query= $query->where('users_details.matrimonial','LIKE','1');

Query builder - codeigniter

I have problems building a query that uses three tables.
If I write the query as:
$query = "select domains.ID, domains.Name, domains.IP,
database_info.DB_Name, database_info.DB_User, database_info.DB_Pass,
ftp.FTP_User, ftp.FTP_Pass from domains
left join database_info on domains.ID = database_info.Domain_ID
left join ftp on domains.ID = ftp.Domain_ID
where domains.ID = $ID";
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
I get error Number 1096 No tables used SELECT *
However is I write the query using the query builder:
$this->db->select('domains.ID','domains.Name','domains.IP',
'database_info.DB_Name','database_info.DB_User',
'database_info.DB_Pass','ftp.FTP_User','ftp.FTP_Pass');
$this->db->from('domains');
$this->db->join('database_info','domains.ID =
database_info.Domain_ID', 'left');
$this->db->join('ftp','domains.ID = ftp.Domain_ID','left');
$this->db->where('domains.ID',$ID);
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
The query runs but only returns the domains.ID
Open to ideas and suggests
try this
$query = $this->db
->select('domains.ID,domains.Name,domains.IP, database_info.DB_Name,database_info.DB_User,database_info.DB_Pass,ftp.FTP_User,ftp.FTP_Pass')
->from('domains')
->join('database_info','domains.ID = database_info.Domain_ID', 'left')
->join('ftp','domains.ID = ftp.Domain_ID','left')
->where('domains.ID',$ID)
->get();
if($query->num_rows()>0)
{
return $query->result();
}
The point is in your select statement - the Query Builder only accepts one argument for your entire select statement not that many as you did
and please for the sake of security - never ever try to execute sql
statements without escaping them - CI's query builder will do this per
default - but your first example is pretty dangerous and widely open
to sql injections
Use db->query
$sql = "select domains.ID, domains.Name, domains.IP,
database_info.DB_Name, database_info.DB_User, database_info.DB_Pass,
ftp.FTP_User, ftp.FTP_Pass from domains
left join database_info on domains.ID = database_info.Domain_ID
left join ftp on domains.ID = ftp.Domain_ID
where domains.ID = $ID";
$query = $this->db->query($sql);
$result = $query->result_array();
Follow the CodeIgniter User Guide

codeigniter join 3 tables and get data from 1st table where 1st table is linked with 2nd table and 2nd table is linked with 3rd table

user_table(user_id, user_name)
user_group_table(user_id, group_id)
group_table(group_id, group_name)
I need to retrieve data (user_id, user_name, group_name)
How can I join these tables in CodeIgniter?
$this->db->query('SELECT user_table.user_id, user_table.user_name,
group_table.group_name
FROM
user_table
join user_group_table on user_group_table.user_id=user_table.user_id
join group_table on group_table.group_id=user_group_table.group_id')
->result_array();
return it and print/use the array however you like OR you can use Codeigniter Query Builder Class
$where = array(); //if u want to use any condition
$this->db->select('user_table.user_id, user_table.user_name, group_table.group_name');
$this->db->join('user_group_table', 'user_group_table.user_id = user_table.user_id');
$this->db->join('group_table', 'group_table.group_id = user_group_table.group_id');
$result = $this->db->order_by('user_table.user_id', 'desc')->get_where('property', $where)->result_array();
return $result
$this->db->select('user_table.user_id,user_table.user_name,group_table.group_name');
$this->db->from('user_group_table');
$this->db->join('user_table', 'user_table.user_id = user_group_table.user_id');
$this->db->join('group_table', 'group_table.group_id = user_group_table.group_id');
$query = $this->db->get();
$res = $query->result();
return $res;

Codeigniter - how to delete row on inner join? [duplicate]

I have a problem with my query and I need to join two tables from different databases now my problem is how can I execute my query. I got my syntax format from here
Please visit first this link so you could understand why my SQL syntax is like this http://www.x-developer.com/php-scripts/sql-connecting-multiple-databases-in-a-single-query
Im using CodeIgniter and here is an Idea of what my query looks like: Notice the way I'm selecting my columns: DATABASE_NAME.TABLE_NAME.COLUMN_NAME
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
$SELECT = "SELECT $ACCOUNTS.BALANCES_TABLE.IDNO, $ACCOUNTS.BALANCES_TABLE.balance";
$FROM = "FROM $ACCOUNTS.BALANCES_TABLE";
$WHERE = "$ACCOUNTS.BALANCES_TABLE.IDNO IN (SELECT $ENROLLEES.ENROLLEES_TABLE.IDNO FROM $ENROLLEES.ENROLLEES_TABLE)";
$SQL = $SELECT ." ". $FROM ." ". $WHERE;
MAIN PROBLEM: How to Execute my query?
If we do like this in codeIgniter:
$ENROLLEES->query($SQL); or $ACCOUNTS->query($SQL);
How can I execute my query that Im having multiple databases? What will I provide here[database]->query($SQL); ?
$sql="Select * from my_table where 1";
$query = $this->db->query($SQL);
return $query->result_array();
If the databases share server, have a login that has priveleges to both of the databases, and simply have a query run similiar to:
$query = $this->db->query("
SELECT t1.*, t2.id
FROM `database1`.`table1` AS t1, `database2`.`table2` AS t2
");
Otherwise I think you might have to run the 2 queries separately and fix the logic afterwards.
I can see what #Þaw mentioned :
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
CodeIgniter supports multiple databases. You need to keep both database reference in separate variable as you did above. So far you are right/correct.
Next you need to use them as below:
$ENROLLEES->query();
$ENROLLEES->result();
and
$ACCOUNTS->query();
$ACCOUNTS->result();
Instead of using
$this->db->query();
$this->db->result();
See this for reference:
http://ellislab.com/codeigniter/user-guide/database/connecting.html
http://www.bsourcecode.com/codeigniter/codeigniter-select-query/
$query = $this->db->query("select * from tbl_user");
OR
$query = $this->db->select("*");
$this->db->from('table_name');
$query=$this->db->get();
return $this->db->select('(CASE
enter code hereWHEN orderdetails.ProductID = 0 THEN dealmaster.deal_name
WHEN orderdetails.DealID = 0 THEN products.name
END) as product_name')
$this->db->select('id, name, price, author, category, language, ISBN, publish_date');
$this->db->from('tbl_books');

MySql SHOW query in ZEND

In zend it is written like
$table = $this->getTable();
$select = $table->select()->where('status = ?',1)
->where('columnOne= ?', 1)
->order('columnTwo')
->limit(1);
similar to where, order, limit conditions how can I condition for LIKE?
My query is
SHOW TABLE STATUS LIKE 'tableName'
I tried in this way
$table = $this->getTable();
$query= $table->select("TABLE STATUS")
->like($table);
$id = mysql_query($query);
then I found that no method for LIKE is available in ZEND.
Then How can I write above query in Zend framerk?
Thanks in advance.
This works for me, so hopefully it should give you what you want:
$stmt = $dbAdapter->query("SHOW TABLE STATUS LIKE '$tableName';");
$tableStatus = $stmt->fetchObject();