How would this SQL join work? - php

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 :)

Related

how to query join table

i have 2 table like this
accounts
-------------
id|
username|
password|
email|
date
bans
-------------
id|
accounts|
ip|
admin|
reason|
date|
for example on bans show result like this
5|NULL|0|1|hayo ketauan|2017-01-17 20:05:5
i want bans.accounts and bans.admin show accounts.username
i have query like this
SELECT `accounts`.`username`, `bans`.`ip`, `bans`.`admin`, `bans`.`reason`, `bans`.`date`
FROM `accounts`
INNER JOIN `bans`
ON `accounts`.`id` = `bans`.`account`
and got result
Naufal|NULL|1.1.1.1|1|hayo ketauan|2017-01-17 20:05:5
i want to like this
Naufal|1.1.1.1|frans|hayo ketauan|2017-01-17 20:05:5
I'm confuse about the query.
To get the username of the admin, you need to JOIN to the accounts table a second time using the admin value of the bans table. This should work:
SELECT `a1`.`username`, `bans`.`ip`, `a2`.`username` AS admin, `bans`.`reason`, `bans`.`date`
FROM `accounts` `a1`
INNER JOIN `bans` ON `a1`.`id` = `bans`.`account`
INNER JOIN `accounts` `a2` ON `a2`.`id` = `bans`.`admin`

Join two table from MySQL database

I need to join table accounts and table users from my MySQL database.
$sql = "SELECT id FROM `".DB_ACCOUNTS."` WHERE `id` IS NOT NULL ";
table accounts and table users contain a same column as id. I want to get gender, birthday from users and bodybuild, weight, height, ethnictype, skincolor from accounts.
Users Table
Accounts table:
How to do this? Please help.
SELECT US.gender, US.birthday,AC.bodybuild, AC.weight, AC.height, AC.ethnictype, AC.skincolor
FROM accounts AC
INNER JOIN users US ON AC.ID=US.ID
You can try above code.
It will helps you.
$sql = "SELECT u.gender, u.birthday, a.bodybuild, a.weight, a.height, a.ethnictype, a.skincolor
FROM accounts a
LEFT JOIN users u on a.id = u.id
WHERE a.id IS NOT NULL ";
please try above query...
Change id column in User table with userid.after that you must add userid at Accounts table as Foreign Key. userid column at Account table contain userid value from User table.
ALTER TABLE Accounts
ADD FOREIGN KEY (userid) REFERENCES Users(userid);
sql = "SELECT u.gender,u.birthday,ac.bodybuild, ac.weight, ac.height, ac.ethnictype, ac.skincolor
FROM Users u
LEFT JOIN Accounts ac
ON u.userid=ac.userid
WHERE ac.userid IS NOT NULL; ";

Select all from another table in select query

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

multiple joins for different ids in same and different tables

I've different tables and I've to fetch data.
order table
orderId, clientId, buyerId, loadingCompanyId, productTypeId, orderStatusId
user table
userId, userCompanyName
orderStatus table
orderStatusId, orderStatusName,
productType table
productTypeId, productTypeName
so I've these tables. In order table clientId, buyerId and loadingCompanyId are coming from user's table userId and I want to fetch its userCompanyName, orderStatusId is coming from orderStatus table and I want to fetch orderStatusName and last one is productTypeName productTypeId is coming from productType table and want to fetch productTypeName but the problem is with clientId, buyerId and loadingCompanyId these three ids are userId from user table so how I've to fetch client, buyer and loading company names.
That are some simple joins with aliases
select *
from order
join user client on clientId = client.userId
join user buyer on buyerId = buyer.userId
join user company on loadingCompanyId = company.userId
join orderStatus on orderStatus.orderStatusId= order.orderStatusId
join productType on productType.productTypeId = order.productTypeId
SELECT O.orderId, UserClient.userCompanyName As clientName, UserBuyer.userCompanyName AS buyerName, UserloadingCompany.userCompanyName AS buyerName, OS.orderStatusName, PT.productTypeName
FROM Order O
INNER JOIN user AS UserClient
ON O.clientId = UserClient.userId
INNER JOIN user AS UserBuyer
ON O.buyerId = UserBuyer.userId
INNER JOIN user AS UserloadingCompany
ON O.loadingCompanyId = UserloadingCompany.userId
INNER JOIN orderStatus AS OS
ON O.orderStatusId = OS.orderStatusId
INNER JOIN productType AS PT
ON O.productTypeId = PT.productTypeId

How to create a contacts page properly?

I want to do a contacts page. Here is my table structure:
Accounts Table:
1) id
2) User_id
3) Full_name
4) Username
5) Email
6) Password
Contacts Table:
1) My_id (This is the user who added friend_id)
2) Contact_id (this is the contact who was added by my_id)
3) Status
My query looks something like this:
$sid = ID of user who is viewing;
$sql = "SELECT STRAIGHT_JOIN DISTINCT contacts.contact_id,
accounts.full_name FROM contacts INNER JOIN accounts
on contacts.contact_id = accounts.user_id WHERE
contacts.my_id = '$sid' OR contacts.contact_id = '$sid'";
The problem is that it doesn't work the right way. I end up seeing my name in the query (meaning when i login i see my name in the contacts instead of contacts name).
How to fix this? Thanks.
The STRAIGHT_JOIN keyword should not be necessary here. To get the contact information, use a second JOIN to the contacts table which relates it back to the accounts.
SELECT
DISTINCT c.contact_id,
cn.full_name
FROM
accounts a
/* first join connects the adding user's id to records in contacts */
INNER JOIN contacts c ON a.user_id = c.My_id
/* second join connects contact list user ids back to names in accounts */
INNER JOIN accounts cn ON cn.user_id = c.Contact_id
WHERE a.User_id = '$sid'
This query ought to suffice:
SELECT DISTINCT contacts.contact_id, accounts.full_name
FROM contacts, accounts
WHERE (contacts.my_id = '$sid' AND contacts.contact_id = accounts.user_id)
OR (contacts.contact_id = '$sid' AND contacts.my_id = accounts.user_id)

Categories