I have three tables
orders
members
products
In orders, I have fields id, mem_id, date, prod_id, status where mem_id coming from members table and prod_id is coming from products table
In members, I have fields mem_id, name, phone, address, city, state, zip, country where country holds id of country from country table
Now, I want to show the records from orders table only for product id 2 and from members from country id 25
I have tried doing:
SELECT o.mem_id, o.prod_id, m.mem_id FROM orders o INNER JOIN members m ON m.mem_id = (SELECT mem_id FROM members WHERE country=25) WHERE o.prod_id=2
But it gives:
Fatal error: Call to a member function fetch_assoc() on a non-object in
So, its not fetching any data and a problem in my query. Please suggest me, Thanks
Join the table using ON condition and apply the where condition like this
SELECT o.mem_id, o.prod_id, m.mem_id
FROM orders o
INNER JOIN members m
ON m.mem_id = o.mem_id
WHERE o.prod_id=2 and m.country=25
You can JOIN the tables on column and specify the condition in WHERE clause, e.g.:
SELECT o.mem_id, o.prod_id, m.mem_id
FROM orders o JOIN members m ON o.mem_id = m.men_id
WHERE o.prod_id = 2 AND m.country = 25;
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 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 two tables:
Student [id, studname, school, bid, ...]
attendance [id, sid, bid, attenDate, ...]
sid: student id
bid: batch id
I am trying to get record from Student table of on batch and attendance of same batch of one particular date.
But I am getting record/list of student whose id are avail in attendance.
Student Table:
Attendance Table:
In both cases, I'm getting only two records. I am expecting 3 student list.
SELECT * FROM students s LEFT OUTER JOIN attendance a ON s.id = a.sid WHERE s.bid=1 AND a.attenDate='2017-03-18'
$condition = ['s.bid'=>$bid,'a.attenDate'=>$adate];
$listattend = $this->db->select('*')
->from('students s')
->join('attendance a', 's.id = a.sid', 'right outer')
->where($condition)
->get();
return $listattend->result();
Try this
SELECT a.*,s* FROM attendance a LEFT JOIN students s ON a.sid = s.id AND a.bid=s.bid WHERE DATE(a.date) = "2017-02-05"
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 do I select specific columns from three different tables based on their primary key.
I have three tables,
Table1 orders (orderID, CustomerID, productID, orderdate, ,order quantity order_description).
Table2 Customers(CustomerID, Fname, Lname, Email).
Table3 Products(ProductID, Productname, Product weight).
I would like a query where I can display a result for a specific order for a specific customer and only the product he orderred. so I would like a new result to display
Customer Fname, lname, product name, quantity, for based on the specific orderID.
Thank you. Hopefully you understood my question, Sorry my English is very bad.
Try this one out:-
SELECT
c.Fname, c.Lname, p.productname, o.quantity
FROM Orders o
JOIN Customers c on o.CustomerID = c.CustomerID
JOIN Products p on p.ProductID = o.ProductionID
WHERE o.OrderID = #OrderID
$sql = "SELECT c.Fname, c.Lname, p.Productname, o.orderquantity
FROM orders o
JOIN Customers c ON o.CustomerID = c.CustomerID
JOIN Products p ON o.productID = p.ProductID
WHERE o.orderID = '$orderID';";
Doing direct variable injection like that is BAD, but without knowing what db connection you're using, that should be enough to get you going in the right direction.