MySQL get multiple result from one table in one row - mysql

The are two tables "costs" and "contacts". Names off all sellers and buyers are in "contacts" table. With following query i retrieve the id of seller and buyer for each item but I want to get their names from "contacts" table
SELECT
costs.id as ID,
costs.idContactPayedBy,
costs.idContactPayedTo
FROM costs
WHERE
costs.idbuilding=286
but I want to get seller and buyers names from contacts table
SELECT
costs.id as ID,
contacts.lastname as seller,
contacts.lastname as buyer
FROM costs , contacts
WHERE
costs.idbuilding=286
and costs.idContactPayedBy = contacts.id
and costs.idContactPayedTo = contacts.id
so the desired result is like this
ID Seller Buyer
21 jackson Brown
29 Bush wilson

SELECT
c.id as ID,
cntby.lastname as seller,
cntto.lastname as buyer
FROM costs AS c
INNER JOIN contacts AS cntby ON c.idContactPayedBy = cntby.id
INNER JOIN contacts AS cntto ON c.idContactPayedTo = cntto.id
WHERE c.idbuilding=286
Note 1: Use INNER JOIN only if idContactPayed[By/To] columns are mandatory (NOT NULL). If these columns allows nulls then you should use LEFT OUTER JOIN. In my opinion, both columns should be mandatory.
Note 2: As a matter of style: please avoid old style joins (ANSI 86/89): FROM table1 a, table2 b WHERE <join condition>.

Related

MySQL, Join two tables with one table?

I have one table customer, and one bill, and one sell.
Customer table
id-----name
Bill table
id-----customer_id
Sell table
id-----customer_id-----bill_id-----qtt-----price
A customer can have the records in the sell table with customer_id, and also have the bill record in the bill table with customer_id and this bill record has record in the sell table with bill_id.
This means a customer can have direct or indirect (in this case by passing the bill table) with sell table.
Now how to join tables that retrieve the total sell of a customer with ascending or deciding order?
Any idea?
I have tried many ways for example something like below, but none of them was working:
SELECT
sell.id AS sell_id,
customer.id,
bill.id AS bill_id,
customer.`name`,
sell.quantity*sell.price AS sell_price
FROM
customer_tb customer
JOIN bill ON bill.customer_id = customer.id
JOIN sell ON sell.customer_id = customer.id OR sell.bill_id = bill.id
NOTE: In case of bill table has a record in the sell table, the customer_id column is NULL and also same for the customer sell record the bill_id is NULL, this means in the sell table in the same entry only one of the (customer_id, bill_id) column has value.
You have the customer column in your sell tables, you don't need to join with bill table.
SELECT
customer.id,
customer.name,
SUM(sell.quantity * sell.price) AS total_amount
FROM customer_tb as customer
INNER JOIN sell
On sell.customer_id = customer.id
GROUP BY customer.id
You have to group your rows by the customer to get their sum independently
Your challenge is to look up the correct Customer.name for each Sell row.
You need to join Sell to Customer, and also join Sell to Bill to Customer.
How to do this?
FROM Sell s
LEFT JOIN Cust c1 ON s.customer_id = c1.id
LEFT JOIN Bill b on s.bill_id = b.id
LEFT JOIN Cust c2 ON b.customer_id = c2.id
That gives us:
s.id: the Sale id
s.quantity, s.price: the business details of the Sale
c1.name: the Customer name directly from the Sale's customer_id
c2.name: the Customer name indirectly through the Sale's bill_id
Because we use LEFT JOINs, either name can be NULL depending on which ids are null. And, I believe you favor using the direct name over the indirect name if you have both.
That means we use COALESCE() to choose one name from the two. Something like this
SELECT s.id, s.Quantity, s.Price,
COALESCE (c1.name, c2.name) name,
COALESCE (c1.id, c2.id) customer_id
FROM Sell s
LEFT JOIN Cust c1 ON s.customer_id = c1.id
LEFT JOIN Bill b on s.bill_id = b.id
LEFT JOIN Cust c2 ON b.customer_id = c2.id
But, if you have a chance to rework this database before you put a ton of data into it, please consider redesigning things to get rid of this ambiguity. It's going to drive you mad and cost you bigger servers if you have to make it work well with megarows.

How to get Data from three tables in one query where table 2 contains foreign keys from table 1 and 3

I want to get all the suppliers for one product with product details for which I am using following tables.
I have one Table products with columns
id(pk)
name
type
second table product_supplier with columns
psid(pk)
pid(fk from products)
sid(fk from supplier)
third table supplier with columns
id(pk)
firstname
lastname
I want to get data from these three tables in one mysql query.
Is this what you are looking for?
select p.*, s.*
from products p
inner join product_supplier ps on ps.pid = p.id
inner join supplier s on s.id = ps.sid
order by p.id, s.id
This will return each product along with all the associated suppliers.

How to get names of relevant ID's from 2 different tables and display them

I have 3 tables, First one has the product ID and Name, Second one has Supplier ID and name, In the 3rd one i have product ID and Supplier ID. While displaying, i want to replace the product ID and supplier ID in the 3rd table with product name and supplier name from the 1st and 2nd table respectively.
Please let me know the query for executing it.
Reference: http://dev.mysql.com/doc/refman/5.7/en/join.html
SELECT * FROM table1
INNER JOIN table2
ON table1.id=table2.id
INNER JOIN table3
ON table2.id=table3.id;
If your tables are named products_master, stockists_master, and stockist_product_offer, then you can join the tables and select any of the six columns that you want.
SELECT product_master.name, stockists_master.name
FROM products_master
INNER JOIN stockist_product_offer
ON product_master.id = stockist_product_offer.product_id
INNER JOIN stockists_master
ON stockist_product_offer.stockist_id = stockist.id;
you have to join the tables on IDs so the query is :
let s say that :
* first table : product
* second table : supplier
* third table : match
SELECT P.PRODUCTNAME
S.SUPPLIERNAME
FROM
PRODUCT P
INNER JOIN
MATCH M
ON P.PRODUCTID = M.PRODUCTID
INNER JOIN
SUPPLIER S
ON S.SUPPLIERID = M.SUPPLIERID
ORDER BY 1
;

MySQL, Show value of column if match exist else leave as null

I'm sure this has been asked before but can't find the answer.
I have 3 tables OWNER, CAR, HOUSE
OWNER has 2 columns id and name
CAR has 3 columns id, ownerId and cartype
HOUSE has 4 columns id, ownerId, address, country
I want to write a SQL query that gets the owners name, cartypes, and addresses that are in Sweden
Here comes the tricky part. I want all the owners names and cartypes in the result table even if they don't own a house in Sweden. Can I get all that in 1 query or do I need to use 2? How would that query look?
You should be able to accomplish this with a simple left join:
SELECT O.name, C.cartype, H.address, H.country
FROM OWNER AS O
JOIN CAR AS C ON O.id = C.ownerid
LEFT JOIN HOUSE AS H ON O.id = H.ownerid AND Ucase(H.country) = "SWEDEN"
This will always give you a list of owners and their car types, in addition, it will give you a list of those that also happen to have a house address in sweden.
First you need to join the table then add new column in query by using CASE to check
SELECT o.* , c.* ,h.*,
(CASE WHEN h.county ='sweden' THEN h.county ELSE NULL END) AS HasCountry
FROM OWNER o
JOIN CAR c ON (c.ownerId =o.id)
JOIN HOUSE h ON (h.ownerId =o.id)

Mysql join query on multiple tables

I have three tables and in those tables these basic fields
contacts
con_id (primary key)
con_name
con_work_id
con_country_id
work
work_id (primary key)
work_company_name
work_country_id
country
country_id (primary key)
country_name
I'm trying to run a query which displays the con_name, work_company_name and then the country name for BOTH the contact and the work company.
I've tried this;
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country ON contacts.con_country_id = country.country_id
LEFT JOIN country ON work.work_country_id = country.country_id
But of course this doesn't work because the last join causes a clash with the second one.
I'm almost there, but can't get the query to display the country_name associated with both the contact AND the work company.
I'd appreciate a way forward.
Many thanks,
Wonder
The following should work:
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country c1 ON contacts.con_country_id = c1.country_id
LEFT JOIN country c2 ON work.work_country_id = c2.country_id
The trick is to add an alias to the table, so that it is possible to distinguish the two.