I want to join 4 tables in codeigniter - php

Till know I join 3 table
$this->db->select('*');
$this->db->from('dispatch_challan');
$this->db->join('challan_bilties', 'dispatch_challan.disp_id = challan_bilties.challan_id');
$this->db->join('bilty', 'challan_bilties.challan_bilties_id = bilty.id');
$this->db->where('dispatch_challan.disp_ch_no',$disp_ch_no);
$query = $this->db->get();
return $query->result_array();
My output is like this after join table
In above image the Consignor and Consignee fetch id but i want name so i want to join 4th table i.e ts_users
In this table the full name of this Consignor and Consignee
ts_users table
user_id user_fullname user_remark
1 abc consignee
2 xyz consignor
3 pqr consignee
4 lmn consignor
I want to get full name based on Consignor and Consignee from 4th table(ts_users)

Since you don't really tell us where the consignor and consignee fields come from, you'll need to fill in the blanks (denoted as xxx below)
All you need is to join twice with the ts_users table. Add these:
$this->db->join('ts_users u1', 'xxx.consignor = u1.user_id');
$this->db->join('ts_users u2', 'xxx.consignee = u2.user_id');
Since you'll be joining twice with the same table, your resultset will be a bit confusing if you stick to select * so I'd recommend fetching only the fields you need. For instance:
$this->db->select('u1.user_id as consignor, u2.user_id as consignee, dispatch_challan.*, challan_bilties.*, bilty.*');
give it a shot and let me know if it works

This is how to join any number of tables in codeigniter without any problems:
$this->db->select("
$table_1.id AS table_1_id,
$table_1.whatever AS table_1_whatever,
$table_2.id AS table_2_id,
$table_2.whatever AS table_2_whatever,
$table_3.id AS table_3_id,
$table_3.whatever AS table_3_whatever,
$table_4.id AS table_4_id,
$table_4.whatever AS table_4_whatever,
");
$this->db->join($table_2, "$table_2.id = $table_1.table_2_id", 'left');
$this->db->join($table_3, "$table_3.id = $table_1.table_3_id", 'left');
$this->db->join($table_4, "$table_4.id = $table_1.table_4_id", 'left');

Related

Count from selection and access both tables with joins

I stumbled upon a query that I have never done until now.
Before asking the question I looked for if another user had had the same need as me but nothing.
My goal is very simple:
having two tables:
collaboratori (collaborators)
invite (invitations)
I have to count how many invitations the collaborators have made.
table structure of collaboratori:
ID_Collaboratori | cod_manager
37 4675
150 6675
3 6575
table structure of inviti:
invite_id | invite_code_manager
37 6675
39 6575
40 4675
41 6675
if I execute the join obviously I access the two tables in this way:
$q_stats_prod_manager = $connessione->prepare("
SELECT * FROM invite
LEFT JOIN collaboratori
ON collaboratori.cod_manager = invite.invite_code_manager ");
$q_stats_prod_manager->execute();
$r_stats_prod_manager = $q_stats_prod_manager->get_result();
my need lies in showing in a table:
show me for each manager who has his cod_manager inside the inviti table, the number of times he sent them.
Name Surname Manager 1 | Number of invite: 200
Name Surname Manager 2 | Number of invite: 50
Name Surname Manager 3 | Number of invite: 10
not limiting myself to just one counter but also being able to access other table values ​​like any join
I take the liberty of putting the answer that was partially written by another user, adding a detail and explanation for future users. The resolution query for this case is the same:
$q_stats_prod_manager = $connessione->prepare("
SELECT count(invite.invite_id)
/*name of what you want to call the result you will see in the while*/
AS result_count, /*you can call this value whatever you want*/
/*Start | Values ​​of the tables you are interested in selecting*/
collaboratori.nome,
collaboratori.data_registrazione,
invite.invite_code_manager
/*End | Values ​​of the tables you are interested in selecting*/
FROM collaboratori
LEFT JOIN invite
ON invite.invite_code_manager = collaboratori.cod_manager group by invite.invite_code_manager
");
$q_stats_prod_manager->execute();
$r_stats_prod_manager = $q_stats_prod_manager->get_result();
$count_invite_manager=mysqli_fetch_array($r_stats_prod_manager);
$number_of_invite_manager = $count_invite_manager[0];
Select the id of the table you want to count
Give a name you wish you want to name the counted result
Select the values ​​of the tables on which you will perform the join you want to view
Join the tables
Show the result with while
Code while:
<?php while($rowstatspm = mysqli_fetch_assoc($r_stats_prod_manager)){ ?>
<!-- this is the fancy name you associated with your query when you wrote: AS nameofwhatyouwant -->
<?php echo $rowstatspm['result_count'] ;?>
<?php } ?>
You seem to want aggregation. I assume you want all rows for collaboratori, so that should be the first table for the LEFT JOIN:
SELECT c.cod_manager, COUNT(i.invite_code_manager)
FROM collaboratori c LEFT JOIN
invite i
ON c.cod_manager = i.invite_code_manager
GROUP BY c.cod_manager;
Your question doesn't describe where the name comes from. But those fields should be in both the SELECT and GROUP BY.
SELECT count(invite.invite_id),collaboratori.name FROM collaboratori
LEFT JOIN invite
ON invite.invite_code_manager = collaboratori.cod_manager group by invite.invite_code_manager

How can i use sql count in these multiple tables?

I am still a php/mysql newbie and I am working on mysql table relationship concept and i am having an issue with using mysql count in multiple table. Here is my db structure.
**product table**
id product_name product_img groupeid
1 Sneaker Mark sneaker_adi.png 1
2 bag Eric bageric.png 2
3 Sneaker Etoi sneakeretoi.jpg 1
**groupe table**
group_id group_name
1 men
2 women
**category table**
catid catname
1 sneaker-shoes
2 bag-woman
**productcategory table**
prod_id cat_ID
1 1
2 2
3 1
What i want to do is to determine the number of sneaker-shoes using mysql.
We can see that the number of sneaker-shoes in the db is 2.
But how can i use **count()** in these multiple tables.
I tried like this;
$sql = "SELECT COUNT(*) product.id,product_name,catname FROM product INNER JOIN productcategory ON product.id = prod_id INNER JOIN category ON catid = cat_ID WHERE catname='sneaker-shoes'";
i got error like:
Fatal error: Call to a member function execute() on a non-object in C:\wamp\www\kbashopping\Homme\index.php on line 32
Hope i exposed the issue clearly, any help and assistance will be appreciate
Thanks
If you are looking only for the count, mention only the count phrase in the Select clause.
Change :
SELECT COUNT(*) product.id,product_name,catname FROM
to :
SELECT COUNT(product.id) FROM
SELECT count (pc.cat_ID) FROM productcategory pc inner join category c on c.catid = pc.cat_ID where c.catname = 'sneaker shoes';
This will build a temporary table in mysql that joins category and product category but only including results where the catname is sneaker shoes. Then it selects a column to run the count operation on, and returns the result of count.

Select from three tables

I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)

MySQL join for CodeIgniter

I have 2 tables. Table kind and table living.
In the table kind, it has id, kind_o & living_id while in living it has id & type.
Example data.
KIND
id kind_o living_id
1 dog 1
2 narra 2
LIVING
id type
1 animal
2 tree
How can I query in CodeIgniter if I want to return ex. SELECT * kind WHERE id = 1 and i want to return also all the data inside living table?
Example output:
id kind_o living_id living.id type
1 dog 1 1 animal
2 tree
Try :
$this->db->select('k.*,l.type');
$this->db->from('kind as k');
$this->db->join('living as l','l.id = k.living_id');
$this->db->where('l.id','1');//use Kind id Here you want to use.
$query = $this->db->get();
$this->db->last_query();
return $query->result_array();
See Document
select k.id as kind_id, k.kind_o, k.living_id, l.id as living_id, l.type FROM kind k join living l on (k.living_id=l.id);
Always prefer to select fields comma seperated then using select *.
select * usually takes time compared to query selecting all required feilds.

select all users where count() equals a specific value

$sql = "SELECT * FROM cars WHERE COUNT(brand)='4'";
$results = mysql_query($sql);
$rows = mysql_fetch_array($results);
print_r($rows);
the table cars has these columns:
id brand price
0 bmw
1 corvette
2 mercedes
3 bmw
4 bmw
5 toyota
6 bmw
7 honda
8 lotus
this is what I am trying to do, return from the table 'cars' every brand that has 4 different cars. In this example bmw has 4
4 different cars is 4 different rows with the same brand name.
so I am trying to echo the name of the brand where the total number of inventory is 4.
I hope I made sense, any help would be appreciated.
EDIT: I tried
SELECT * FROM cars LEFT JOIN users ON cars.user_id=users.user_id HAVING count(user_id) = 4 this is not working any ideas?
SELECT brand FROM cars GROUP BY brand HAVING COUNT(brand) = 4
For you edit:
SELECT t1.brand, t2.email
FROM cars t1
LEFT JOIN users t2 ON t1.user_id = t2.user_id
GROUP BY t1.brand HAVING COUNT(t1.brand) = 4
I think in the query like you have you need to use having
SELECT * FROM cars HAVING COUNT(brand)=4;
and group by depending on what you grouping
You want perform an aggregate function (COUNT) on multiple rows at once. Pretty much anytime you want to perform multiple counts, you should think GROUP BY.
$sql = "SELECT brand FROM cars GROUP BY brand HAVING COUNT(*) >= 4";
If you do SELECT * in this query, you'll get 1 random row for each brand (probably the one with the lower id).
The HAVING clause will act as a WHERE on each of the groups.

Categories