Column 'company_id' in field list is ambiguous
It does not seem "ambiguous", I have no idea where I should fix it:
SELECT company_id, companies.name
FROM company_contracts AS contracts
LEFT OUTER JOIN companies ON companies.id = contracts.company_id
LEFT OUTER JOIN order_logs AS logs ON companies.id = logs.company_id;
Because company_id appears on both table company_contracts and order_logs,you need to specify it
SELECT contracts.company_id,c1.name
FROM company_contracts as contracts
LEFT OUTER JOIN companies as c1 on c1.id = contracts.company_id
LEFT OUTER JOIN order_logs as logs on c1.id = logs.company_id;
You should qualify all columns names in such a query. In addition, if you really want outer joins, the second join condition should refer to the first table, not the second:
SELECT cc.company_id, c.name
FROM company_contracts cc LEFT OUTER JOIN
companies c
ON c.id = cc.company_id LEFT OUTER JOIN
order_logs ol
ON cc.company_id = ol.company_id;
Or, more likely, you want to keep all companies and the query should look like:
SELECT c.id, c.name
FROM companies c LEFT OUTER JOIN
company_contracts cc
ON c.id = cc.company_id LEFT OUTER JOIN
order_logs ol
ON c.id = ol.company_id;
Related
I am trying to create normalization 3 nf (normal form) in this database. However, when I execute the query, the table is empty. As you will see, this is my table and diagram.
Here is the relationship that we want to 3NF
Torder->(Customer-Food-Carrier-Waiter)-(Ternary Relationship)-(Customer_id,Food_id,Carrier_id,Waiter_id)
Here is my query
SELECT
Customers.ID AS CustomerID,
Food.ID AS FoodID,
Carrier.ID AS CarrierID,
Waiter.ID AS WaiterID,
tOrder.ID AS TORDERID
FROM
tOrder
INNER JOIN
Customers ON Customers.ID = tOrder.Customer_id
INNER JOIN
Food ON Food.ID = tOrder.Food_id
INNER JOIN
Carrier ON Carrier.ID = tOrder.Carrier_id
INNER JOIN
Waiter ON Waiter.ID = tOrder.Waiter_id
ORDER BY tOrder.ID;
Clearly, you have an issue where some of the tables are empty or the ids do not match. You can use LEFT JOIN to keep all orders and see what is happening:
FROM tOrder o LEFT JOIN
Customers c
ON c.ID = o.Customer_id LEFT JOIN
Food f
ON f.ID = o.Food_id LEFT JOIN
Carrier ca
ON ca.ID = o.Carrier_id LEFT JOIN
Waiter w
ON w.ID = o.Waiter_id
If there are no matches, then the orders are still in the results, with NULL for the values in the table with no matches.
Note that this also introduces table aliases, so the query is easier to write and to read.
I have the following structure for table categories:
And I'm trying to do a self join on parentId = id to get the parent category but I also want the query to include the row without the join.
I tried doing:
select *
from `project.dataset.categories` c
left join `project.dataset.categories` p1 on p1.id = c.parentId or p1.id = c.id
But this throws
LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
For reasons I can't understand.
So basically I'm expecting something like this in the end:
Sadly using union is not an option because this is just a simplification of a much more complex query.
Any help appritiated.
I think you can do:
select cc.*
from `project.dataset.categories` c cross join
unnest(array[id, parent_id]) c_id left join
categories cc
on cc.id = c_id
-- where c.id = 2106 -- you seem to want this condition as well
This creates an array with the two columns you want to join -- and then unnests them into two separate rows for the join.
Or just use or and exists:
select c.*
from `project.dataset.categories` c
where c.id = 2106 or
exists (select 1
from `project.dataset.categories` c2
where c2.id = 2016 and c2.parentid = c.id
);
This is my sql select statment
SELECT k.name, c.name AS nameCustomer, o.*
FROM offertes o
INNER JOIN customers k
ON o.idCustomer= k.id
INNER JOIN contactperson c
ON o.idContact = c.id;
When o.idContact doesn't exist, then there will be no values selected. I wan't to get NULL instead of nothing. It still need to SELECT the whole row! Can anyone help me?
ps. I think it's going wrong with the inner join (ON o.idContact = c.id);
Try this:
Replace your last INNER JOIN with LEFT JOIN.
Using LEFT JOIN you tell my main table (offertes) returns always result but if in secondary table (contactperson) there's no row matches returns NULL all fields of that table
SELECT k.name, c.name AS nameCustomer, o.*
FROM offertes o
INNER JOIN customers k
ON o.idCustomer= k.id
LEFT JOIN contactperson c
ON o.idContact = c.id;
You'll need an outer join instead.
e.g.
SELECT k.name, c.name AS nameCustomer, o.*
FROM customers k LEFT OUTER JOIN
(offertes o INNER JOIN
contactperson c
ON o.idContact = c.id)
ON o.idCustomer= k.id;
(I assume there'll be a counterpart of an offerte in contactperson, else you'll need an outer join there as well)
you need to rewrite a query using outer join
SELECT k.name, c.name AS nameCustomer, o.*
FROM customers k
LEFT OUTER JOIN offertes o ON o.idCustomer = k.id
LEFT OUTER JOIN contactperson c ON o.idContact = c.id;
I have 3 tables.
First one:
Stores(StoreID, Name)
Second one:
Store_Items(StoreID, ItemID)
Third one:
Items(ItemID, Name, Price)
How can I write a single statement, which get all StoreID and all names and all items for each StoreID?
Should I use Join or something?
select *
from Store as S
left outer join Store_Items as SI on SI.StoreID = S.StoreID
left outer join Items as I on I.ItemID = SI.ItemID
select s.storeid, s.name, si.itemid, i.name, i.price
from store s
left join store_item si on si.storeid = s.storeid
left join item i on i.itemid = store.itemid
Yes, you should join the tables roughly as such
SELECT * FROM `firstone`
LEFT OUTER JOIN `secondone`
ON firstone.StoreId = secondone.StoreID
LEFT OUTER JOIN `thirdone`
ON secondone.ItemID = thirdone.ItemID
I need to go to two tables to get the appropriate info
exp_member_groups
-group_id
-group_title
exp_members
-member_id
-group_id
I have the appropriate member_id
So I need to check the members table, get the group_id, then go to the groups table and match up the group_id and get the group_title from that.
INNER JOIN:
SELECT exp_member_groups.group_title
FROM exp_members
INNER JOIN exp_member_groups ON exp_members.group_id = exp_member_groups.group_id
WHERE exp_members.member_id = #memberId
SELECT g.group_title
FROM exp_members m
JOIN exp_member_groups g ON m.group_id = g.group_id
WHERE m.member_id = #YourMemberId
If there is always a matching group, or you only want rows where it is, then it would be an INNER JOIN:
SELECT g.group_title
FROM exp_members m
INNER JOIN
exp_member_groups g
ON m.group_id = g.group_id
WHERE m.member_id = #member_id
If you want rows even where group_id doesn't match, then it is a LEFT JOIN - replace INNER JOIN with LEFT JOIN in the above.