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
Related
I have six tables
The main table Orders is an encrypted table containing orders that customers have made.
The second table is a hash table for the Orders table. OrdersHash.
The remaining four tables are store tables that all have the same structure. StoreA, StoreB, StoreC, & StoreD.
Orders Table
orderId | rest of row...
OrdersHash Table
orderId | orderIdHash | rest of row..
The four store tables all share this structure.
orderIdHash | customerId | rest of row..
Using only the customerId I am trying to query the four store tables to see if any of the store tables contain the customerId. If the customerId is found on any of the four store tables I want to use the orderIdHash to get me back to the original Orders table and return the row for any orders that were found.
If I use a the customerId for Mike I would expect row 1 from the Orders table.
This is what I have tried so far.
"SELECT
o.dateShipped AS orderShipped,
o.shipped AS shipped,
o.recieved AS recieved
FROM Orders o
JOIN OrdersHash oHash
ON o.orderId = oHash.orderId
JOIN StoreA a
ON ohash.orderIdHash = a.orderIdHash
JOIN StoreB b
ON ohash.orderIdHash = b.orderIdHash
JOIN StoreC c
ON ohash.orderIdHash = c.orderIdHash
JOIN StoreD d
ON ohash.orderIdHash = d.orderIdHash
WHERE
a.customerId = :customerId1
OR b.customerId = :customerId2
OR c.customerId = :customerId3
OR d.customerId = :customerId4";
**customerId 1,2,3,4 are all the same value.. I have to use a different name for binding in PDO.
This will return a result but it seems to return the same row from Orders for every row in a store with a matching orderIdHash when I just need the one record from the Orders table.
Thank you in advance for your help.
It seems you probably want to UNION the store results and then JOIN that to the Orders table. By using UNION rather than UNION ALL, we can select only the distinct orderIdHash values, ensuring we only get one row for each Order in the result table. Something like this:
SELECT o.dateShipped AS orderShipped,
o.shipped AS shipped,
o.recieved AS recieved
FROM (SELECT customerId, orderIdHash
FROM (SELECT customerId, orderIdHash FROM StoreA
UNION
SELECT customerId, orderIdHash FROM StoreB
UNION
SELECT customerId, orderIdHash FROM StoreC
UNION
SELECT customerId, orderIdHash FROM StoreD
) stores
WHERE customerId = :customerId) c
JOIN OrdersHash oHash ON oHash.orderIdHash = c.orderIdHash
JOIN Orders o ON o.orderId = oHash.orderId
I have a query that looks up people's full names based on their record ID's in a table called users. The full names are tied to their roles in another table (table1). This requires multiple joins to the users table:
SELECT table1.id, users.full_name AS "Requester",
users.full_name AS "Approver,"
users.full_name AS "Ordered By",
users.full_name AS "Received By"
FROM table1
JOIN users AS users
ON table1.requester_id = users.id
JOIN users AS users2
ON table1.approver_id = users2.id
JOIN users AS users3
ON table1.ordered_by = users3.id
JOIN users AS users4
ON table1.received_by = users4.id
WHERE table1.deleted_record !=1;
The problem I'm having is with ordered_by and received_by. Often, they don't yet exist, because the order has neither been ordered nor received, so the ID for each can be 0, which has no corresponding value in the userstable. When I run this query, I should get back all 475 records that exist, but I only get back 365, because of those 0 values. How can I modify this query to make sure all rows are returned, even if ordered_by and/or received_by = 0?
First, your primary table driving the query should be table1. Then, you are using JOIN instead of LEFT JOIN. LEFT JOIN will give you a null result if no link, but not fail. In which case, you might have to use an IF for your fields value
SELECT table1.id, req.full_name AS "Requester",
app.full_name AS "Approver",
ordr.full_name AS "Ordered By",
rec.full_name AS "Received By"
FROM table1
LEFT JOIN users AS req
ON table1.requester_id = req.id
LEFT JOIN users AS app
ON table1.approver_id = app.id
LEFT JOIN users AS ordr
ON table1.ordered_by = ordr.id
LEFT JOIN users AS rec
ON table1.received_by = rec.id
WHERE table1.deleted_record !=1;
This should do it
You are looking for left join:
SELECT t1.id, ur.full_name AS "Requester",
ua.full_name AS "Approver,"
uo.full_name AS "Ordered By",
urv.uo AS "Received By"
FROM table1 t1 LEFT JOIN
users ur
ON t1.requester_id = ur.id LEFT JOIN
users ua
ON t1.approver_id = ua.id LEFT JOIN
users uo
ON t1.ordered_by = uo.id LEFT JOIN
users urv
ON t1.received_by = urv.id
WHERE t1.deleted_record <> 1;
Note that I changed the aliases on the users references from fairly meaningless u1, u2, etc. to ua, uo, and so on. Also, these need to be used in the SELECT to get the right full name.
I need to display details from 2 different tables
Table 1- user table
Table 2-bicycle table
I can show a list of users and bicycle.
But can't show a list that show which bicycle belongs to who.
Bicycle table,
user table
You need to use a LEFT JOIN. The query below will connect your user table to your bicycle table based on the userIDand return all of the bicycle and user fields
SELECT b.*, u.*
FROM registered_users u
LEFT JOIN registered_bicycle b ON (u.userID = b.userID)
If you want specific bicycle fields just prefix them with b. Example:
SELECT b.brand, b.model, b.color...
Click on SQL tab and type this:
SELECT * FROM `registered_bicycle` JOIN `registered_users` ON `registered_users`.`userID` = `registered_bicycle`.`userID`
and run it
You have to make a foreign key from user_table to bicycle_table like this.
user_table has user_id = user_1,name=John Richard
bicycle_table has user_id = user_1,description = Bicycle1
to fetch the data:
select a.user_id, a.name,b.description from
(select user_id, name from user_table) as a
left join
(select user_id,description from bicycle_table) as b
on a.user_id = b.user_id
I have a "manager" that can be assigned to many locations and handles one department in every location. I want a query that can grab all of this:
http://sqlfiddle.com/#!2/33453c/1
In the example above (in the link) you can see I have calculated how many employees are in each department.
Managers and Employees are in a table named staff, I do not want the query to retrieve the manager record(s). So user_role = "Employee";
I run this as a raw query in laravel so I can retrieve them as objects:
$employees= DB::query('query goes in here')->get();
An example would be the manager with staff_id '5' get every employee that is in all of the locations and department the manager is part of if that makes sense?
My guess would be:
Pseudo
First Query SELECT ALL FROM staff, locations and departments where user_role = "employee"
Second Query SELECT ALL FROM staff, locations and departments where manager id=5
Remove all results that do not satisfy the second query but join both queries together?
Help would be greatly appreciated.
I think this is what you want. Left join the manager's department on employee's departments, and get the count of "employee" staff members in each of those departments (even if 0)
Below, shown for manager #2 (you could eliminate the where clause and group by manager id and dept id if you want to see all managers)
SELECT dept.name AS dept, loc.address1 AS loc, emp.*
FROM staff AS mgr
INNER JOIN department_staff AS mgr_dept
ON mgr_dept.staff_id = mgr.staff_id
INNER JOIN departments AS dept
ON mgr_dept.dept_id = dept.dept_id
INNER JOIN location_staff AS mgr_loc
ON mgr_loc.staff_id = mgr.staff_id
INNER JOIN locations AS loc
ON mgr_loc.loc_id = loc.loc_id
INNER JOIN (
SELECT emp.*, dept.dept_id, loc.loc_id
FROM staff AS emp
INNER JOIN department_staff AS dept
ON emp.staff_id = dept.staff_id
INNER JOIN location_staff AS loc
ON emp.staff_id = loc.staff_id
WHERE emp.user_role = "Employee"
) AS emp
ON emp.loc_id = mgr_loc.loc_id
AND emp.dept_id = mgr_dept.dept_id
WHERE mgr.staff_id = 2
I have 2 tables: "shares" and "pending_share_changes". The "shares" table has the following columns:
share_ID, asset_ID, member_ID, percent_owner, is_approved
pending_share_changes has the following columns:
share_change_ID, asset_ID, member_ID, percent_owner, is_approved, requested_by
I have a form (php) which lists the pending changes which the members have to approve or deny. Everything works fine unless the member has no existing shares, which means there is no record in the "shares" table for them. The query I am using to view the pending share changes is as follows:
SELECT p.share_change_ID, assets.asset_desc, assets.asset_value, shares.percent_owner AS
percent_owner_old, p.percent_owner
FROM pending_share_changes as p
inner join assets on assets.asset_ID = p.asset_ID
inner join shares on shares.asset_ID = p.asset_ID AND shares.member_ID = p.member_ID
INNER JOIN orgs ON orgs.org_ID = assets.org_ID
WHERE orgs.org_ID = '$org_ID' AND p.member_ID = '$member_ID' AND p.is_approved = '0'
I tried using IFNULL(shares.percent_owner, 0) AS percent_owner_old but that didn't work. The query returns no results. I would like to have the percent_owner_old column display a "0" if there is no record in the shares table.
Thanks.
Try changing your join to shares from an INNER JOIN to a LEFT JOIN.
SELECT ....
FROM pending_share_changes p
....
LEFT JOIN shares ON shares.asset_ID = p.asset_ID AND shares.member_ID = p.member_ID
This means "for every member_ID and asset_ID in p, join it to the matching row in shares. If the member_ID/asset_ID exists in p but not shares, make a row anyway and set the corresponding values to NULL".
The INNER JOIN means to only show joined rows where the relevant record exists in both tables.
Then you can combine with the IFNULL(shares.percent_owner,0) AS percent_owner_old.