I'm trying to extend a 2 table join query to join 3 tables. I'm using PDO
SELECT tb.product, COUNT(tb.ID) AS nums FROM items tb LEFT JOIN itemCode cod ON tb.ID = cod.codeID WHERE tb.product = :product AND cod.name = :cName
The above 2 table join works. My question is, how do I match itemCode's availabilityID column to tblC's stockID column and add it to the WHERE clause with same query to get the result?
So basically I want to check if the availabilityID column of itemCode table match with stockID column of tblC table.
EDIT: table structure:
**items table**
ID | product
**itemCode table**
codeID | name
**tblC table**
stockID
As stated in your question you want to match availabilityID with stockID, this is what the query does. However, the availabilityID does not show up in your edit.
SELECT tb.product, COUNT(tb.ID) AS nums
FROM items tb
LEFT JOIN itemCode cod ON tb.ID = cod.codeID
JOIN tblC stock ON stock.`stockID` = cod.`availabilityID`
WHERE tb.product = :product AND cod.name = :cName
Related
I have a table named Records that shows products. I also have a table named Categories that shows the categories for each individual product (if one exists).
The Categories table is structured liked:
id category_id
-- -----------
1 1
1 3
3 1
3 2
5 4
The query I run to pull record ID and category ID(s) is:
SELECT
Records.id,
(SELECT
GROUP_CONCAT(C.category_id)
FROM `Categories` C
WHERE Records.id = C.id) AS 'CategoryName'
FROM
Records
The output will return:
id CategoryName
-- ------------
1 1,3
2 NULL
3 1,2
4 4
5 NULL
I have an area of my website where users can filter records by category. Let's say user wants to filter for category = 1 or 2. I was thinking I just tack on a WHERE FIND_IN_SET(1,CategoryName) OR FIND_IN_SET(2,CategoryName) but this does not work because of the MySQL execution order and CategoryName column does not exist yet.
What is the best way to filter for category_id? The input for categories will be comma separated but I can use PHP to explode() the string to separate them.
You can rewrite the query with a LEFT join of Records to Categories:
SELECT r.id,
GROUP_CONCAT(c.category_id) AS CategoryName
FROM Records r LEFT JOIN Categories c
ON c.id = r.id
GROUP BY r.id
and if you want to use the same query for filtering all you have to do is add at the end a HAVING clause:
HAVING FIND_IN_SET(1, CategoryName) OR FIND_IN_SET(2, CategoryName)
Or, you can filter first and then aggregate:
SELECT r.id,
GROUP_CONCAT(c.category_id) AS CategoryName
FROM Records r INNER JOIN Categories c
ON c.id = r.id
WHERE c.category_id IN (1, 2)
GROUP BY r.id
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, 1st table holds the categories list and 2nd table holds the profile information along with 1st table category id as a foreign key. As follow
Table1
id CATEGORY
1 first
2 second
Table2
id CATEGORY name phone
1 2 John 9999999999
how to retrieve table 2 records along with category name (not id:2 as shown in table 2)
I tried this,
SELECT category, name, phone FROM table2;
I need to see following line as result
second, john, 9999999999
get me out of this step, thanks in advance.
What you need is a JOIN, which means you "combine" two tables by linking rows from one table to rows from another table based on some criterion.
In your case, the criterion is that the value of CATEGORY in table2 must equal the value of ID in table1, which is expressed as follows:
SELECT table1.category,
table2.name,
table2.phone
FROM table2
JOIN table1
ON table2.category = table1.id
If needed, you can add a WHERE clause to limit the result to specific rows, e.g. WHERE table1.id = 9999999999 would filter the rows that have category 9999999999.
This should work:
SELECT t1.category, t2.name, t2.phone
FROM table2 AS t2
JOIN table1 AS t1
ON t1.id = t2.category
You can make an INNER JOIN and get the category from the Table 1, like this:
SELECT tb1.category, tb2.name, tb2.phone
FROM table2 tb2
INNER JOIN table1 tb1 on tb2.category = tb1.id
WHERE tb2.id = 1;
Hope it helps!
I have two mysql tables called room_type and resve_room.
The room_type table has room numbers 5, 3, 6 ,9 ,10. The resve_room table has room numbers 3 and 9.
How can I write an SQL query that filters out reserved room number 3 and 9 from the room_type table. I want to return the following room numbers 5,6,10.
I have tried using the following SQL but it only returned 3 and 9 :
SELECT room_type.room_no
FROM room_type,in_hand_room
WHERE in_hand_room.room_no=room_type.room_no
&& room_type.room_id='$room_id
Try something like this
SELECT B.Accountid
FROM TableB AS B
LEFT
JOIN TableA AS A
ON A.ID = B.Accountid
AND A.ID IS NULL;
Or maybe this
select ids from TableB EXCEPT select id from TableA
Try the follwoing
SELECT room_type.room_no
FROM room_type
WHERE room_type.room_no NOT IN (SELECT resve_room.room_no from resve_room )
or in_hand_room instead of resve_room beacuse it 's not clear from your desc which one is your reservation table
I have two tables: product and url_alias
product table has two columns: product_id and model
url_alias table has two columns: product_id and keyword
So, product_id is unique & primary key for both tables.
Now, I want to add (append) info of product.model to url_alias.keyword if product_id of both tables is same.
For example:
product.product_id = 123
product.model = 987
product.product_id = 123
url_alias.keyword = my-first-book.html
Desired result = 987-my-first-book.html
How can I do it?
PS: '-' hyphen is also required to add with model.
Try CONCAT::
Select
product.product_id,
CONCAT(product.model,'-', url_alias.keyword)
from
product
inner join url_alias on (url_alias.product_id=product.product_id)
SELECT CONCAT(product.model, '-', url_alias.keyword) AS url
FROM product
INNER JOIN url_alias
ON product.product_id = url_alias.product_id
WHERE product.product_id = '123'
SELECT CONCAT(p.product_id,' - ',u.keyword ) AS res
FROM product AS p
LEFT JOIN
url_alias AS u USING (product_id)
WHERE p.product_id = 123
Note : Remove the WHERE condition if you want to get result for all the products