I have following 3 tables(Organization, OrganizationAddress, Address).Each organizations has more than one address and one of them is primary.if an organization has primary address,I need to select the primary address else that column can be null or blank.
How can i write a sql sub query in mysql?.
Please help me,
Thank you,
You just need a LEFT JOIN
select a.orgid, b.orgaddressid, c.address
from organization a
left join organization_address b on a.orgid = b.orgid and b.isprimaryaddress = 'YES'
left join address c on b.orgaddressid = c.addressid
where a.orgid = 1;
Maybe something like this. I think you need to use LEFT JOIN for both tables:
select
organization.orgid,
organization_address.orgaddressid,
address.address
from
organization
LEFT JOIN organization_address
ON organization_address.orgid = organization.orgid
AND organization_address.isprimaryaddress = 'YES'
LEFT JOIN address
ON organization_address.orgaddressid = address.addressid;
A LEFT JOIN selects rows in the left table and matching rows in the right table if there are matching rows in the right table. If there aren't any matching rows in the right table the query will return NULL for the columns from the right table.
SELECT o.OrgId, oa.OrgaddressId, a.Address FROM Organization AS o
LEFT JOIN 'Organization Address' AS oa ON oa.OrgId=o.OrgId
LEFT JOIN Address AS a ON a.AddressId=oa.OrgAddressId
WHERE oa.IsPrimaryId='YES';
Sub Query is slower then join query, so i just inform to you please ignore sub query
if you want to get this type of result you can use Left join
select a.orgid, b.orgaddressid, c.address
from organization a
left join organization_address b on a.orgid = b.orgid and b.isprimaryaddress = 'YES'
left join address c on b.orgaddressid = c.addressid
where a.orgid = 1;
Left join means
returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
visit the Types of join in google you can get more idea
Related
I'm populating a table which is fetching the ids from 2 other tables to display their information, for example, delivery has a Hamburguer and the box, but the user might register the delivery with out the box, only with the hamburguer.
When I make a INNER JOIN SELECT to get the data from the DB it will return 0 results since there is no box and I'm trying to compare the ids that don't exist. It doesn't populate the table then.
SELECT
entrega_telemovel.*,
telemovel.id_telemovel,
telemovel.nroserie,
nro_telemovel.numero_telemovel,
nro_telemovel.id_nrotelemovel,
funcionarios.id_funcionario,
funcionarios.nome
FROM entrega_telemovel
INNER JOIN telemovel
ON entrega_telemovel.telemovel = telemovel.id_telemovel
INNER JOIN nro_telemovel
ON nro_telemovel.id_nrotelemovel = entrega_telemovel.numero_telemovel
INNER JOIN funcionarios
ON funcionarios.id_funcionario = entrega_telemovel.funcionario_entrega
ORDER BY funcionarios.nome;
In this query above entrega_telemovel.telemovel=telemovel.id_telemovel the value in entrega_telemovel.telemovel is null like the example I gave above. So 0 results are returned from the query.
How can I solve this ?
You are looking for a LEFT JOIN.
INNER JOIN only combines rows, that exist in both tables. A LEFT JOIN on the other hand always produces at least one row. If on table does not have a match for it, all columns are set to NULL.
SELECT
entrega_telemovel.*,
telemovel.id_telemovel,
telemovel.nroserie,
nro_telemovel.numero_telemovel,
nro_telemovel.id_nrotelemovel,
funcionarios.id_funcionario,
funcionarios.nome
FROM entrega_telemovel
LEFT JOIN telemovel
ON entrega_telemovel.telemovel = telemovel.id_telemovel
LEFT JOIN nro_telemovel
ON nro_telemovel.id_nrotelemovel = entrega_telemovel.numero_telemovel
LEFT JOIN funcionarios
ON funcionarios.id_funcionario = entrega_telemovel.funcionario_entrega
ORDER BY funcionarios.nome;
You want to show all entrega_telemovel entries, no matter whether they have a match in entrega_telemovel or not. This is what an outer join does.
SELECT ...
FROM entrega_telemovel et
LEFT OUTER JOIN telemovel t ON et.telemovel = t.id_telemovel
...
I have 3 tables, errorcode_table, description_table, and customer_table.
The query below will display all records that are in the errorcode_table and I have an inner join that will also display the customer_table as per the serial number in both tables.
SELECT
errorcode_table.error,
errorcode_table.deviceserialnumber,
customer_table.serialnumber,
customer_table.customer,
FROM errorcode_table
INNER JOIN customer_table
ON errorcode_alert_table.deviceserialnumber = customerinfo_table.serialnumber
Now I want to also display the description of the error code as well, here's my attempt:
SELECT
errorcode_table.error,
errorcode_table.serialnumber,
customer_table.serialnumber,
customer_table.customer,
description.serialnumber
description.info
FROM errorcode_table
INNER JOIN customer_table
RIGHT JOIN description_table
ON errorcode_table.deviceserialnumber = customer_table.serialnumber
ON errorcode_table.deviceserialnumber = description_table.serialnumber
Now I'm not getting any records. Please assist.
The ON clause for each join should appear immediately after each join condition. And you can introduce table aliases to make the query easier to read.
SELECT
e.error,
e.serialnumber,
c.serialnumber,
c.customer,
d.serialnumber,
d.info
FROM errorcode_table e
INNER JOIN customer_table c
ON e.deviceserialnumber = c.serialnumber
RIGHT JOIN description_table d
ON e.deviceserialnumber = d.serialnumber;
I need a help in the mysql query.
SELECT `main_table`.*, `lea`.`account_id`
FROM `lists_list` AS `main_table`
INNER JOIN `list_account` AS `lea` ON lea.list_id=main_table.id
INNER JOIN `list_customer` AS `customer` ON main_table.id = customer.list_id
WHERE ((`customer.customer_id` = '1') OR (`lea.account_id` = '1'))
Now my problem is, I need to show lists that have a link type of "B" / "C" that have no list accounts in list_account and list_customer table , if the customer's Account matches that type in lists_list table
Could you please help in this.
That is actually pretty easy, just use LEFT JOIN instead of INNER JOIN:
SELECT `main_table`.*, `lea`.`account_id`
FROM `lists_list` AS `main_table`
LEFT JOIN `list_account` AS `lea` ON lea.list_id=main_table.id
LEFT JOIN `list_customer` AS `customer` ON main_table.id = customer.list_id
WHERE ((`customer.customer_id` = '1') OR (`lea.account_id` = '1'))
An INNER JOIN requires a record on the left and right side to exist, while LEFT JOIN will also return records where the right side does not exist (leaving the fields from that table with NULL as their value).
I have the following INNER JOIN statement and It is only returning results if all four tables have a match for the order number in them.
I need it to include every result in the main table KC_Orders regardless of the equivalent contents of each INNER JOIN tables in the $sql
I understand that this is the point of the INNER JOIN but I need it do something else.
$sql = "SELECT *
FROM `KC_Orders`
INNER JOIN `KC_Payments`
ON KC_Orders.orderNumber = KC_Payments.orderNumber
INNER JOIN `KC_OrderStatus`
ON KC_Orders.orderNumber = KC_OrderStatus.orderNumber
INNER JOIN `KC_Statuses`
ON KC_OrderStatus.statusID = KC_Statuses.statusID";
$AllOrders = $db->query($sql);
Use left outer joins
SELECT *
FROM
`KC_Orders`
LEFT JOIN `KC_Payments`
ON KC_Orders.orderNumber = KC_Payments.orderNumber
LEFT JOIN `KC_OrderStatus`
ON KC_Orders.orderNumber = KC_OrderStatus.orderNumber
LEFT JOIN `KC_Statuses`
ON KC_OrderStatus.statusID = KC_Statuses.statusID
If there is always a status available, you can keep the inner join for the KC_Statuses table
SELECT *
FROM
A
LEFT JOIN B
ON A.id = B.id
... means that all the records from A will be returned and only the records from B that match a record from A. Records from A are returned even when there is no matching record in B.
It sounds like you want an OUTER JOIN rather than an INNER JOIN.
If you want all rows from the KC_Orders table, then put that table first in the FROM clause, and use a LEFT JOIN on the other tables. (The OUTER keyword is not required.) This will return all rows from the KC_Orders table, even if no matching row is found in the other tables. NULL values will be returned in place of value from "missing" rows.
SELECT *
FROM `KC_Orders`
LEFT
JOIN `KC_Payments`
ON KC_Orders.orderNumber = KC_Payments.orderNumber
LEFT
JOIN `KC_OrderStatus`
ON KC_Orders.orderNumber = KC_OrderStatus.orderNumber
LEFT
JOIN `KC_Statuses`
ON KC_OrderStatus.statusID = KC_Statuses.statusID
I have a litte problem with a mysql query.
I use 5 tables:
user_has_data (uid, dataid); users (uid, uname); user_in_group(uid, groupid, data); groups(groupid, data, packageid); packages(packageid, name)
all ids are PK. I want to build a sql query that finds a user, which belongs to a specified dataid, by its uname and checks if the user is in a group (relation in table user_in_group) belonging to a specified package (a group is assigned to one package). if so data from users, package and group should be fetched, otherwise only the user data should be fetched. Therefore I use left joins, so I can also get the users with no group:
SELECT `uac`.`uid`, `u`.`uid`, `uig`.`groupid`, `ag`.`packageid`
FROM `user_has_data` AS `uac`
INNER JOIN `users` AS `u` ON u.uid = uac.uid
LEFT JOIN `user_in_group` AS `uig` ON uig.uid = uac.uid
LEFT JOIN `groups` AS `ag` ON (ag.groupid = uig.groupid) AND (ag.packageid = 2)
WHERE (uac.dataid = '3') AND (u.uname LIKE 'test%')
GROUP BY `u`.`uid`
Unfortunately I get wrong results: I get groups that have a different packageid than stated in the join, if the user has another group assigned to him with a different packageid.
probably this is because the first left join has no restrictions to packageid and the second is a left join and so it has no restrictions on the result (packageid is NULL for all results, but should have values). If I change the second left join to a ordinary join, the group problem would be fixed but the query cant find users without group any more.
Any ideas how to fix this or even possible?
thanks in advance!
Are you saying that you are actually seeing the value ag.packageid = 2 in your query results?
If not, I think you might try something like:
SELECT `uac`.`uid`, `u`.`uid`, `g`.`groupid`, `g`.`packageid`
FROM `user_has_data` AS `uac`
INNER JOIN `users` AS `u` ON u.uid = uac.uid
LEFT JOIN (`user_in_group` AS `uig`
INNER JOIN `groups` AS `ag` ON (ag.groupid = uig.groupid) AND (ag.packageid = 2) )
AS `g` ON uac.uid = g.uid
WHERE (uac.dataid = '3') AND (u.uname LIKE 'test%')
GROUP BY `u`.`uid`
Because you are limiting your search to a specific group packageid of '2', why not just make both of your LEFT JOIN INNER JOINS and then throw in ag.packageid = 2 in your WHERE clause?
SELECT `uac`.`uid`, `u`.`uid`, `uig`.`groupid`, `ag`.`packageid`
FROM `user_has_data` AS `uac`
INNER JOIN `users` AS `u` ON u.uid = uac.uid
LEFT OUTER JOIN `user_in_group` AS `uig` ON uig.uid = uac.uid
LEFT OUTER JOIN `groups` AS `ag` ON ag.groupid = uig.groupid
WHERE (uac.dataid = '3') AND (u.uname LIKE 'test%')
AND (ag.packageid = 2 OR uig.uid IS NULL)
GROUP BY `u`.`uid`
I know LEFT JOIN and LEFT OUTER JOIN mean the same thing, but I like to be explicit. With the condition in your join, I bet you were getting groups with different packages, but weren't getting the packages?