I have 2 tables that I am attempting to join but to only display rows of data based on specific parameters.
Table one: "orders"
Table two: "cash_tracking"
Matching parameters from both tables: "orders.user_id" and "cash_tracking.user_id" and the user_id is 2640
The result I want to see is all the "orders.name" from the "user_id=2640" on a specific shop "shop_id = 7777" and based on a specific register number from the "cash_tracking" table eg. "cash_transaction_reg=444454"
However, when I run my query it shows me every single order on this shop instead of the ones related to the user 2640 on this specific register - 444454
Here is what I have so far:
SELECT orders.name
FROM orders
Inner JOIN cash_tracking
ON orders.user_id = cash_tracking.user_id
WHERE orders.shop_id = 7777
AND cash_tracking.user_id=2640
AND cash_tracking.cash_transaction_reg=444454
You select all orders for user_id, shoud be some join orders.Id = cash_tracking.OrderId
Related
I'm trying to search one table for entities with a certain field in one table and use fields within that entity to to join it with two other tables specific fields
first I search for entities with CustomerID == 2 and use their other fields to search to other tables
Using BoxTypeID from those specific entities I want to find the corresponding BoxType
Using WarehouseID from those specific entities I want to find the corresponding name
Once I have found all three fields (BoxID, BoxType, name) I want to display them all in one table
I have some SQL that works but only displays the BoxID and Warehouses.name. I can get it to also display BoxID and BoxType however not all three at once.
SELECT Boxes.BoxID, Warehouses.name AS Warehouse
FROM Boxes
INNER JOIN Warehouses
ON Boxes.WarehouseID = Warehouses.WarehouseID
WHERE Boxes.CustomerID = 2;
resulting table from query above
However once I try to apply another JOIN it doesn't work
SELECT Boxes.BoxID, Warehouses.name AS Warehouse, BoxTypes.BoxType as Size
FROM Boxes
INNER JOIN Warehouses
ON Boxes.WarehouseID = Warehouses.WarehouseID
INNER JOIN BoxTypes
On Boxes.BoxTypeID = BoxTypes.BoxTypeID
Where Boxes.CustomerID = 2;
Hi guys I'm new with databases and I'm trying to make a query where I join 3 tables. I could make it and I want to clean up the result. I want to know how can I delete the column "pin" from users table and maybe some "ids" columns.
Select * from "wish-list"
Join products
On "wish-list".id = products.holiday_id
Join users
On "wish-list".user_id = users.id
Where "wish-list".id = 1
You need to specify which columns you really need in your output. At the moment you are using
SELECT * which outputs all columns of all joined tables.
Here is what it should look like:
SELECT holiday, products.description, users.pin FROM "wish-list"
JOIN products ON "wish-list".id = products.holiday_id
JOIN users ON "wish-list".user_id = users.id
WHERE "wish-list".id = 1
It's important that you reference all columns which are not your main entity (here wish-list) with tablename.column (products.description and not only description). It will work without referencing strictly but only if the column name is unique in your query.
Furthermore you can rename columns. This is useful for example if you want to get the id's of the product table and the wish-list table.
SELECT product.id AS product_id, id AS wishlist_id FROM "wish-list"
...
Hope that helps!
I have a list of phone numbers that am sending messages to. in table A I record all the delivery status of each number.
I created another table B and stored some contacts in it.
I want an SQL query that I can use to compare the data in A and B such that, I want to get delivery status of numbers in table B from table A. If a number is appearing in the table A and B then I get the delivery status of that number.
I am using this SQL query:
SELECT address, delivery_status FROM safaricom_receipt
WHERE address IN (SELECT mobile FROM saf_dispute);
This will work for you :
SELECT address, delivery_status
FROM safaricom_receipt
INNER JOIN saf_dispute
ON safaricom_receipt.address=saf_dispute.mobile;
Assuming you are using SQL Server; this should work for you
SELECT sd.mobile, sr.delivery_status
FROM saf_dispute sd with (nolock)
LEFT OUTER JOIN safaricom_receipt sr with (nolock) on sd.mobile=sr.address
where sr.delivery_status is NOT NULL
you would get duplicate rows if you are storing multiple rows for the same mobile number in any table. I would suggest adding a datetime column in the future
I have two tables that I try to join over one field and it gives me different results in two queries that should give same results. Queries are:
SELECT * FROM tblCustomer tca
WHERE tca.PhoneNumber IN(
SELECT ts.SubscriptionNumber FROM sub.tblSubscription ts
WHERE ts.ServiceTypeID=4
AND ts.SourceID=-1
)
and
SELECT tca.*
FROM sub.tblSubscription ts
inner JOIN tblCustomer tca
ON ts.SubscriptionNumber = tca.PhoneNumber
WHERE ts.ServiceTypeID = 4
AND ts.SourceID = -1
How is this possible?
I'm assuming a customer can have multiple subscriptions, right? Let's assume you have 5 customers, each with 2 subscriptions...
When doing a SELECT ... FROM Customer WHERE IN (Subscription), you will receive 5 customer records, because each of those 5 customers are in fact in the subscription table, even though the subscription table will have 10 records. You are inherently asking the database for the data from one table, where the value of one of it's fields exists in another table. So it will only return the distinct records in the FROM table, irrespective of the amount of data in the WHERE IN table.
On the other hand, INNER JOINing the Customer table with the subscription table will return 5 customers x 2 subscriptions each = 10 records. By JOINing the tables you are asking the database for ALL the data in each table, where the data is matched up against specific fields.
So yes, the 2 queries will definitely give you different results.
I am using the following query to obtain some sales figures. The problem is that it is returning the wrong data.
I am joining together three tables tbl_orders tbl_orderitems tbl_payment. The tbl_orders table holds summary information, the tbl_orderitems holds the items ordered and the tbl_payment table holds payment information regarding the order. Multiple payments can be placed against each order.
I am trying to get the sum of the items sum(mon_orditems_pprice), and also the amount of items sold count(uid_orderitems).
When I run the following query against a specific order number, which I know has 1 order item. It returns a count of 2 and the sum of two items.
Item ProdTotal ProdCount
Westvale Climbing Frame 1198 2
This order has two payment records held in the tbl_payment table, which is causing the double count. If I remove the payment table join it reports the correct figures, or if I select an order which has a single payment it works as well. Am I missing something, I am tired!!??
SELECT
txt_orditems_pname,
SUM(mon_orditems_pprice) AS prodTotal,
COUNT(uid_orderitems) AS prodCount
FROM dbo.tbl_orders
INNER JOIN dbo.tbl_orderitems ON (dbo.tbl_orders.uid_orders = dbo.tbl_orderitems.uid_orditems_orderid)
INNER JOIN dbo.tbl_payment ON (dbo.tbl_orders.uid_orders = dbo.tbl_payment.uid_pay_orderid)
WHERE
uid_orditems_orderid = 61571
GROUP BY
dbo.tbl_orderitems.txt_orditems_pname
ORDER BY
dbo.tbl_orderitems.txt_orditems_pname
Any suggestions?
Thank you.
Drill down Table columns
dbo.tbl_payment.bit_pay_paid (1/0) Has this payment been paid, yes no
dbo.tbl_orders.bit_order_archive (1/0) Is this order archived, yes no
dbo.tbl_orders.uid_order_webid (integer) Web Shop's ID
dbo.tbl_orders.bit_order_preorder (1/0) Is this a pre-order, yes no
YEAR(dbo.tbl_orders.dte_order_stamp) (2012) Sales year
dbo.tbl_orders.txt_order_status (varchar) Is the order dispatched, awaiting delivery
dbo.tbl_orderitems.uid_orditems_pcatid (integer) Product category ID
It's a normal behavior, if you remove grouping clause you'll see that there really are 2 rows after joining and they both have 599 as a mon_orditems_pprice hence the SUM is correct. When there is a multiple match in any joined table the entire output row becomes multiple and the data that is being summed (or counted or aggregated in any other way) also gets summed multiple times. Try this:
SELECT txt_orditems_pname,
SUM(mon_orditems_pprice) AS prodTotal,
COUNT(uid_orderitems) AS prodCount
FROM dbo.tbl_orders
INNER JOIN dbo.tbl_orderitems ON (dbo.tbl_orders.uid_orders = dbo.tbl_orderitems.uid_orditems_orderid)
INNER JOIN
(
SELECT x.uid_pay_orderid
FROM dbo.tbl_payment x
GROUP BY x.uid_pay_orderid
) AS payments ON (dbo.tbl_orders.uid_orders = payments.uid_pay_orderid)
WHERE
uid_orditems_orderid = 61571
GROUP BY
dbo.tbl_orderitems.txt_orditems_pname
ORDER BY
dbo.tbl_orderitems.txt_orditems_pname
I don't know what data from tbl_payment you are using, are any of the columns from the SELECT list actually from tbl_payment? Why is tbl_payment being joined?