mysql join 2 user_ids in a row from users table - php

There are 2 tables:
orders:
id requester_id supplyer_id status
1 423 1 reserved
2 500 1 supplied
3 222 2 reserved
...
users
id username register_date
1 admin 2012-01-01
2 smith 2013-01-01
...
423 John 2012-10-11
500 Doe 2012-12-11
222 name 2012-10-13
...
I want to join these two tables and get this as a result
id requester_username supplier_username status
1 John admin reserved
2 Doe admin supplied
3 name smith reserved
I can actually join these tables using active records like :
$this->db->select('orders.*,users.username')
->from('orders')
->join('users','users.id = orders.requester_id')
->get()->result();
but I dont know how to get the supplier username at the same time.

Try adding a second JOIN:
$this->db->select('orders.*,requesters.username,suppliers.username')
->from('orders')
->join('users AS requesters', 'requesters.id = orders.requester_id')
->join('users AS suppliers', 'suppliers.id = orders.supplier_id')
->get()->result();

In MySql Your query should be like -
Select a.id, b.username, c.username, a.status
from orders a, users b, users c
where
a.requester_id = b.id
And
a.supplyer_id = c.id
Thanks

Related

MYSQL select query giving duplicated listing

I need some help with this query. I have 2 tables of data
Booking table
bookingid
booking_date
booking_start
staffid
studentid
status
1
2021-10-10
7.30pm
1
12345678
ended
2
2021-10-10
11.30am
1
12345679
ended
3
2021-10-10
12.00pm
1
NULL
cancelled
Student table
|studentid|firstname|lastname|
|--|--|--|
|12345678|john|doe|
|12345679|mary|doe|
|12345670|vincent|doe|
What table im looking for
booking_date
booking_start
studentname
2021-10-10
7.30pm
john doe
2021-10-10
11.30pm
mary joe
2021-10-10
12.00pm
NULL
Using this query,
Select Booking_date,
Booking_start,
case WHEN booking.StudentID is NULL THEN NULL ELSE student.First_name end as First_name,
case WHEN booking.StudentID is NULL THEN NULL ELSE student.Last_name end as Last_name,
BookingID
from booking, student
where (booking.staffid = '$userid') ORDER BY booking_start ASC)
This is the table i am getting
booking_date
booking_start
studentname
2021-10-10
7.30pm
john doe
2021-10-10
7.30pm
mary doe
2021-10-10
7.30pm
vincent doe
2021-10-10
11.30pm
mary joe
2021-10-10
11.30pm
john joe
2021-10-10
11.30pm
vincent joe
2021-10-10
12.00pm
2021-10-10
12.00pm
There should be 1 more 2021-10-10|12.00pm|| im unable to show it due to formatting issues.
It shows duplicated listing with the wrong student name.
What can I do to fix this query?
You need to join tables like this :
booking inner join student on booking.StudentID = student.StudentID
Select Booking_date,
Booking_start,
student.First_name,
student.Last_name,
BookingID
from booking inner join student on booking.StudentID = student.StudentID
where (booking.staffid = '$userid')
ORDER BY booking_start ASC
if you dont join tables, they act like cartesian joins, all rows are matched with all others, so it generates duplicate . for example , if you have 2 table with 10 rows each, in result you get 10*10 result ...
Inner join = you get only if records are matched.
If you have records on booking table and it has no studing_id matched with students table , and still want to display it as a result, you need to use LEFT JOIN instead of INNER JOIN
Also its the best to use join syntax instead of old joins like :
old syntax :
student,booking
where
student.id = booking.studentid
new syntax:
student inner join booking on student.id = booking.studentid

SQL Join raw query issue on similar coloumn

Here is my report table
report
id user_id field_tech_id
1 1 4
2 3 6
And User table is
user
id name user_type
1 raj 1
3 ram 1
4 anthony 2
6 kumar 2
Here in coloumn user_type 1 for user and 2 for field_tech
How can i do join and get the username and field tech name of the orders
I tried like
$data = Report::select('user.name as user_name')
->leftjoin('users','users.id','=','report.user_id')
->get();
But when i try
$data = Report::select('user.name as user_name')
->leftjoin('users','users.id','=','report.user_id')
->leftjoin('users','users.id','=','report.field_tech_id')
->get();
How can i get the user name and field tech name ?
try this :
$data = Report::select('user.name as user_name')
->leftjoin('users as users1','users.id','=','report.user_id')
->leftjoin('users as users2','users.id','=','report.field_tech_id')
->select('users1.*,users2.*)
->get();
If you can pass the whole SQL query then this should work:
SELECT u.name as user_name, r.field_tech_id
FROM user u
LEFT JOIN report r
ON u.id = r.user_id

How to apply searching on two table using mysql

Hi i have two table one is parentlist and childlist.
I have have to apply searching on this table by parentname and childname. I have provided my table structure for better understanding, I have to apply searching on two different field of with different field name.
Fieldname is name in parentlist table and childname in childlist table.
I want below output if I type va then parentlist and childlist record should come in that query like below example. With this va Srting i have a
parentname varu123 and childname varu123 so I want these two record after executing the query.
This is the name of First table with fieldname
parentlist
............................................................
id name mobilenumber user_jid email
............................................................
1 varu123 123456 abc21 abc#gmail.com
2 abhishesk 123456 abc21 def#gmail.com
3 harsh 1234 def22 123#gmail.com
This is the name of Second table with fieldname
childlist
..........................................
id user_id childname Shoolname
...........................................
1 1 ram St.paul
2 1 raj St.xavier
3 2 varu123 St.paul
4 2 arun St.xavier
5 3 kapil St.paul
6 3 kamal St.xavier
I want this output: .
........................................................................................................
id name mobilenumber user_jid email childname Shoolname
..........................................................................................................
1 varu123 123456 abc21 abc#gmail.com ram,raj St.paul,St.xavier
2 abhishesk 123456 abc21 def#gmail.com varu123,arun St.paul,St.xavier
select pl.*, GROUP_CONCAT(cl.childname), GROUP_CONCAT(cl.Shoolname)
from parentlist as pl
inner join childlist as cl on pl.id=cl.user_id
where pl.name like '%va%' or cl.childname like '%va%'
use MySQL inner join or where :-
select * from parentlist inner join
childlist on parentlist.id=childlist.user_id
where childname='varu123' or parentlist.name='varu123'
use MySQL inner join or like :-
select * from parentlist inner join
childlist on parentlist.id=childlist.user_id
where childname like '%varu123%' or parentlist.name like '%varu123%'

Return all the rows even the joined table has empty results

In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .

How do I write SQL to get objects

I have a table in a MySQL table called persons
id LastName FirstName
1 Hansen Timoteivn
2 Svendson Tove
3 Pettersen Kari
and another MySQL table called orders.
id OrderNo personID
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
How can I write a SQL query that I feed into PHP's mysql_query() function to return a list of "Order objects" that each contain a "Person object?" Each "Person object" has first name and last name as properties.
this query will return orders by a certain person (this will not give the object)
SELECT a.ID, a. FirstName, a.LastName, b.OrderNo
FROM Persons a INNER JOIN Orders b ON
a.ID = b.PersonID
WHERE a.ID = 1

Categories