I have two tables:
reviews
company_id (column)
employees
company_id (column)
email (column)
i have many reviews and many employees.
I have a select like this:
$stmt = "SELECT * FROM reviews WHERE user_id = :user_id";
My question: In the same select query, how can i select all (multiple) the employees's email using the company id from the reviews table?
You need to JOIN the two tables
SELECT *
FROM reviews
JOIN employees ON reviews.company_id = reviews.company_id
WHERE user_id = :user_id
SELECT * here will return all the columns of both the reviews and employees tables.
Why do you want using company_id from reviews table when employees table already has it. Anyway You can join 2 table together to resolve your issue. Like this:
SELECT email
FROM employees e JOIN reviews r ON e.company_id = r.company_id
WHERE r.company_id = :company_ud
Related
I have an SQL table of tens of thousand of orders, each one is from around 100 different companies. I want to do a total for each company on an admin web page.
Am I supposed to loop and do 100 queries (one for each company) - eg 'SELECT SUM(order_amount) FROM orders WHERE company = XXX' or do one query to show all orders to loop through them and add each order to an array key of a company, eg company_array[company] += order_amount
With a structure like this :
companies
--------------
company_id
company_name
orders
--------------
order_id
order_company # Foreign key to companies.company_id
order_amount
You can do an unique SQL request like that :
SELECT ALL company_name, SUM(order_amount) AS company_amount
FROM orders INNER JOIN companies ON order_company = company_id
GROUP BY company_name
With PHP :
$sql = <<<SQL
SELECT ALL company_name, SUM(order_amount) AS company_amount
FROM orders INNER JOIN companies ON order_company = company_id
GROUP BY company_name
SQL;
$result = $pdo->query($sql)->fetchAll();
// Now you can loop $result
print_r($result);
I have two related tables: Contracts with 3 columns: ContractsID, AreaManager and AreaLeader. Then I have Employees table with 2 columns: EmployeesID, EmployeeName. The EmployeeID is both foreign key for AreaManager and AreaLeader. I am trying to create a SELECT query to echo ContractID, Area Manager's name and Area Leader's name.
This is what I have;
$query = "SELECT Contracts.ContractsID, Contracts.AreaLeader, Contracts.AreaManager, Employees.EmployeeName FROM Contracts
INNER JOIN Employees ON Employees.EmployeeID = Contracts.AreaManager
INNER JOIN Employees ON Employees.EmployeeID = Contracts.AreaLeader
However, the query doesn't work. I believe that I should use table aliases but I kind of struggling with this.
I tried this but it didn't work:
$query = "SELECT c.ContractsID, m.Employees.EmployeeName as ManagerName, l.Employees.EmployeeName as LeaderName
FROM c.Contracts
JOIN Employees m ON m.EmployeeID = c.AreaManager
JOIN Employees l ON l.EmployeeID = c.AreaLeader
Any help would be greatly appreciated!
The reason why your original query does not work is because you join with Employees twice, and you need to alias them to make a distinction.
$query = "SELECT Contracts.ContractsID, Contracts.AreaLeader, Contracts.AreaManager, Employees.EmployeeName FROM Contracts
INNER JOIN Employees Employee1 ON Employee1.EmployeeID = Contracts.AreaManager
INNER JOIN Employees Employee2 ON Employee2.EmployeeID = Contracts.AreaLeader
You do not need to alias Contracts unless you want to do so.
Your second query fails because you did not alias Contracts properly. You should have put Contracts c instead of c.Contracts.
You have the correct approach on your second query, but you have syntax error in it. your query would look something like this:
$query = "SELECT c.ContractsID, m.EmployeeName as ManagerName, l.EmployeeName as LeaderName
FROM Contracts c
JOIN Employees m ON m.EmployeeID = c.AreaManager
JOIN Employees l ON l.EmployeeID = c.AreaLeader"
I have two tables, "Users" and "Jobs".
Users Table:
UserID
Username
Password
AccountType
Forename
Surname
ContactNumber
EmailAddress
Jobs Table:
JobID
JobTitle
JobDescription
JobLocation
JobDate
JobStartTime
JobPay
PostedDate
PostedBy
AcceptedBy
JobStatus
I want to display all of the job rows, and the Username, ContactNumber and EmailAddress from the users table where the "AcceptedBy" in the jobs table is the same as the "UserID" from the users table.
Try this query
SELECT * FROM Jobs INNER JOIN Users ON Jobs.AcceptedBy = Users.UserID
I'll show you other example and try to do with your example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
here is the tutorial
enjoy :)
I have a data structure where students and groups have many-to-many relationship. I have three tables
students: id, name
groups: id, name
students_groups: student_id, group_id
How do I select only students who are not in a specific group (e.g. group.id = 1)?
I did some searching and tried using sub query but only get an empty set...
select * from students where not exists (select students.* from students left join students_groups on students_groups.student_id = student.id where students_groups.group_id = 1);
how should I query? thx much in advance!
EDIT
OK, it seems the following two finally works... can anyone EXPLAIN to me why I don't need to join table for it to work???
select * from students where not exists (select * from students_groups where students_groups.student_id = student.id and student_groups.group_id = 1);
select * from students where id not in (select student_id from students_groups where group_id = 1);
Using a NOT IN should work fine:
SELECT * FROM Students
WHERE Id NOT IN (
SELECT Student_Id FROM Students_Groups
WHERE Group_Id = 1)
The edited question asks for an explanation.
Think of SQL queries as Venn Diagrams in text. Each clause either defines a circle of content, or tells you which part of the full overlapping circles diagram you're interested in.
select * from students where id not in (select student_id from students_groups where group_id = 1);
One circle is the students table. One circle is the student_groups table where group_id = 1. The circles overlap where students.id equals student_groups.student_id. You want the part of the students table that is not in the overlap area.
You don't need to join the tables because your result set contains data only from the students table. You are using the other table to limit that result set, not provide data to your results.
Untested, but one of the following ought to work. You'll have to do some explaining and see which one is best.
select *
from students
where not exists (select *
from students_groups
where students_groups.student_id = student.id
and students_groups.group_id = 1)
or...
select *
from students
where id not in (select student_id
from students_groups
where group_id = 1)
or...
select students.id, students.name
from students
left outer join students_groups on students.id = students_groups.student_id
and students_groups.group_id = 1
where students_groups.student_id is null
group by students.id, students.name
You could try something like this:
SELECT
*
FROM
students
WHERE
id NOT IN
((SELECT
student_id
FROM
students_groups
WHERE
group_id = 1
))
while ($row = mysql_fetch_array($results)){
$row['fieldName']
}
$query = "SELECT SQL_CALC_FOUND_ROWS *
FROM realestate
INNER JOIN users ON id_user = users.id
LEFT JOIN pic ON id_realestate = realestate.id
{$place}
{$offer}
{$type}
{$price}
group by realestate.id
ORDER BY {$order}
LIMIT 0,10";
i have two tables users and realestate. both have the field id. what should be in the fileldname above to get id in users table?
You should name the fields in the query: if you do users.id AS user_id, then PHP will see a user_id field.
Of course, in this particular case it doesn't matter, since you have two equivalent fields: id_user and id_realestate.