Is it possible to join 2 tables from different database?
Here is my query
public function getschedule($section){
$this->dbsections->select('*');
$this->dbsections->from($section);
//I want to join the column "teacher" of the "section_name" table that is in the "dbsections" database
//to the "id" column of the "teachers" table in the "dbusers" database
$this->dbsections->join('teachers', 'teachers.ID = '.$section.'.TEACHER');
$query = $this->dbsections->get();
$query = $this->dbsections->get();
return $query->result_array();
}
This code gives me error, obviously. I also tried
$this->dbsections->join('dbusers.teachers', 'teachers.ID = '.$section.'.TEACHER');
and
$this->dbsections->join('dbusers.teachers', 'teachers.ID = dbsections.'.$section.'.TEACHER');
But both gives me error
Error Number: 1096
No tables used
SELECT *
You need table name in select * as
$this->dbsections->select("$section.*");// write tour table name before *
And Remove one time
// $query = $this->dbsections->get();
Related
Im searching for a function in MySQL in order to Select rows from multiple tables that have a similar name. For example: Proyect_1, Proyect_2, Proyect_3
All of the tables have the same column names, the only difference between the tables is the table name. It starts with the prefix 'proyect'. The issue is that the program doesn´t know how many 'proyect' tables there are, so i can´t make a list of them and select data like always
I need something like this:
SELECT mydata FROM TABLES LIKE 'Proyect_%';
Any ideas? Thanks!
To get all tables with a common prefix
SHOW TABLES LIKE 'Proyect_%';
This will return rows of tables that matched the prefix. Example:
Proyect_1
Proyect_2
Proyect_3
In PHP you can create a UNION query that will pick up the tables returned by the above query,
$sql = "SHOW TABLES LIKE 'Proyect_%'";
$result = $conn->query($sql);
$dataQuery = array();
$query = "";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_array(MYSQLI_NUM)) {
$dataQuery[] = "SELECT * FROM {$row[0]}";
}
$query = implode(' UNION ', $dataQuery);
}
echo $query;
if you want to search for all tables with name like Proyect then you can get from MySQL information schema.
SELECT * FROM information_schema.tables
From here you can find table by table name
I can't seem to get this working and it's driving me nuts.
I'm using CodeIgniter's Ignited Datatables library I found on github
I have the below method for getting records from a table joined to another:
public function get_templates_ajax() {
$this->datatables->select('tem.template_id, tem.name, tem.vat, COUNT(itm.template_id) AS total_items');
$this->datatables->from('templates tem');
$this->datatables->join('items itm', 'tem.template_id = itm.template_id', 'left');
$this->datatables->where('tem.user_id', $this->session->user_id);
$this->datatables->group_by('tem.template_id');
$this->datatables->add_column('actions', 'EditDelete', 'tem.template_id, tem.name, tem.vat');
return $this->datatables->generate();
}
Yes, I have a field named user_id in both tables, but I'm not selecting the column, however, I get this error: Duplicate column name 'user_id'.
I specified the columns I want selected as seen below:
$this->datatables->select('tem.template_id, tem.name, tem.vat, COUNT(itm.template_id) AS total_items');
I know if I wanted to select the user_id column, I'd use an alias, but I'm not selecting it.
Doing $this->db->last_query(); dumped this SQL:
SELECT COUNT(*) FROM (SELECT * FROM `templates` `tem` LEFT JOIN `items` `itm` ON `tem`.`template_id` = `itm`.`template_id` WHERE `tem`.`user_id` = '1' GROUP BY `tem`.`template_id`) SqueryAux
So it seems that the select method is being ignored while it is selecting all columns from both tables.
What could I be doing wrong?
After much digging, I solved the problem by making some adjustments to the library.
Apparently, the problem was originating from the get_total_results(); method in the class, more specifically these 2 line:
$subquery = $this->ci->db->get_compiled_select($this->table);
$countingsql = "SELECT COUNT(*) FROM (" . $subquery . ") SqueryAux";
The subquery variable was selecting all columns from the main table and joined tables using dear old *.
Here is what I did to fix the problem:
I replaced the $subquery line with this:
....
//ensure table with alias does not contain more than 1 space between original table name and alias
$this->table = preg_replace('!\s+!', ' ', $this->table);
$tbl = explode(" ", $this->table);
//if table has alias, get it, else get table name
$table = count($tbl) > 1 ? $tbl[1] : $tbl[0];
$subquery = $this->ci->db->get_compiled_select($this->table);
//prefix table/alias to * so it selects columns from the main table only
$subquery = str_replace("*", "{$table}.*", $subquery);
....
Hope it helps someone.
I am trying to make a select to my table 'detail' where I have 4 registers with id 0,1,2,3 but this only returns the ids 1,2,3. What will be?
$sqls = "
SELECT *
FROM details
WHERE ped = 4500088849";
$results = mysqli_query($conn, $sqls);
while($row = mysqli_fetch_array($results)) {
print $row['ebelp'];
}
My table
Your query is filtering for rows where the column ped contains the value 11.
Using the mysql command line perform the query
SELECT ped, ebelp FROM details;
And review the value in the ped column for register 0, it sounds like it has a value other then 11.
Using the query SELECT * FROM details will get you all rows with all columns from the details table.
I tried to run query on multiple database with following sql query:
// Collect clients db name
$dbs = array();
foreach($cafes as $cafe)
{
$dbs[] = $cafe->db_name; // client_7
}
// Run multi database query
foreach($dbs as $db)
{
$this->db->select("$db.orders.*,(select count($db.order_items.id) from $db.order_items where $db.order_items.siparis_id = orders.id) as urun_sayisi, (select sum($db.order_items.tutari) from $db.order_items where $db.order_items.siparis_id = orders.id) as price");
}
$this->db->get()->result();
In above example, i have cafe clients and each client have own db named ex: client_7. I am trying to do list all client orders. I got following error with above query:
Unknown table 'client_7' in field list
How can list all rows on orders table from multiple database?
Please mention database name ahead your table name..
for example
SELECT userid FROM db1.user;
SELECT userid FROM db2.user;
Try this:
<?php
$secound_db= $this->load->database('database_two', TRUE);
$query = $secound_db->get('person');
var_dump($query);
Source
I am using Codeigntier to run the a query like this:
$query = $this->db->query("SELECT users.*, user_profiles.* FROM users, user_profiles WHERE " . "users.id = $user_id AND user_profiles.user_id = $user_id");
$row = $query->row();
I have placed an echo to get the last query $this->db->last_query(). Which shows:
SELECT users.*, user_profiles.* FROM users, user_profiles WHERE users.id = 6850 AND user_profiles.user_id = 6850
The result it returns is:
object(stdClass)[24]
public 'id' => string '6849' (length=4)
The id the result shows is 6849. I nearly fell of my chair! I ran the same query on my MySQL database and the id returned for that exact same query is 6850. 6850 is the correct ID.
I have no idea how to debug this as this is the simplest of queries. Any help appreciated.
Explicitly select your columns, and AS *something* alias them. Then check the ID's.
It is likely that the id of the table user_profiles is being shown rather than the actual user id from the users table that you expect as you are not specifying the columns.