I have a query
SELECT DISTINCT c.camp_id AS camp_id,
c.camp_key AS camp_key,
c.camp_active AS camp_active,
c.camp_deleted AS camp_deleted,
c.camp_name AS camp_name,
c.camp_cpc AS camp_cpc,
c.camp_destination AS camp_destination,
camp_token1,
camp_token2,
camp_token3,
camp_token4,
camp_token5,
camp_token6,
camp_token7,
camp_token8,
camp_token9,
camp_token10,
token1_field,
token2_field,
token3_field,
token4_field,
token5_field,
token6_field,
token7_field,
token8_field,
token9_field,
token10_field,
group_name,
group_id,
source_id,
source_name,
user_name
FROM mt_campaigns c
LEFT JOIN mt_offers USING (camp_id)
LEFT JOIN mt_groups USING (group_id)
LEFT JOIN mt_traffic_sources USING (source_id)
LEFT JOIN mt_account WHERE c.owner_id = mt_account.user_id
WHERE camp_deleted=0
Now I want to join another table(mt_account) which has a column called user_id and it matches with the the owner_id column of the mt_canpaigns.
How can i edit the join query?
replace
LEFT JOIN mt_account WHERE c.owner_id=mt_account.user_id
for
LEFT JOIN mt_account ON c.owner_id=mt_account.user_id
You need to use ON to define the fields you want to join on instead of WHERE
Related
I have two tables
Table1: violations
Columns: date, time, pdid, pname, v1, v2, v3, v4
Each v1 through v4 has an integer value which corresponds to a single entry (ID) in table 2.
table2: parking_violations
columns: code, section, description, ID
I need to query each record of violations based on pdid, and match each 'v1-v4' to column 'ID' in the p_violations table.
SELECT
parking.date,
parking.time,
parking.pname,
parking_violations.code,
parking_violations.section,
parking_violations.description
FROM
parking
INNER JOIN parking_violations ON parking.v1=parking_violations.ID
WHERE
pdid=5
This returns the correct records for V1, but I cannot figure out how to also return V2-V4 all populated by matching the value to ID.
Like #fa06 explain, you can use multiple joins to the same table, but instead of inner join i will use left join, this way i have the flexibility of get rows where not all vN have matching IDs on the table parking_violations.
SELECT
parking.date,
parking.time,
parking.pname,
pv1.code,
pv1.section,
pv1.description,
pv2.code,
pv2.section,
pv2.description,
pv3.code,
pv3.section,
pv3.description,
pv4.code,
pv4.section,
pv4.description
FROM
parking
LEFT JOIN
parking_violations AS pv1 ON pv1.ID = parking.v1
LEFT JOIN
parking_violations AS pv2 ON pv2.ID = parking.v2
LEFT JOIN
parking_violations AS pv3 ON pv3.ID = parking.v3
LEFT JOIN
parking_violations AS pv4 ON pv4.ID = parking.v4
WHERE
parking.pdid = 5;
use join multiple time with alias like below:
SELECT
parking.date,
parking.time,
parking.pname,
parking_violations.code,
parking_violations.section,
parking_violations.description
FROM
parking
INNER JOIN parking_violations ON parking.v1=parking_violations.ID
inner join parking_violations a ON parking.v2=a.ID
inner join parking_violations b ON parking.v3=b.ID
inner join parking_violations c ON parking.v4=d.ID
WHERE
pdid=5
SELECT
fromData.name as fromname, toData.name as toName, prodData.prodname,
t1.`from_id`, t1.`to_id` , t1.`product_id` , t1.`title`, t1.`message`, t1.`senttime` , t1.`readstatus`, t1.`responded`, t1.`merchanthidden`
FROM `inquiries` as t1
INNER JOIN users as fromData on t1.from_id = fromData.id
INNER JOIN users as toData on t1.to_id = toData.id
INNER JOIN products as prodData on t1.product_id = prodData.id
WHERE t1.id=13
Above query joins 3 tables (inquiries, users, products) together and gets data from each table.
Sometimes it is possible that items in the 'products' table get deleted. Trying to join products table by a deleted product id will fail the query.
Is there a way that I can assign 'prodData.prodname' a default value and execute query without failing in case of a missing item in products table ?
Why don't use left join insted of inner join ,
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.
SELECT
fromData.name as fromname, toData.name as toName, prodData.prodname,
t1.`from_id`, t1.`to_id` , t1.`product_id` , t1.`title`, t1.`message`, t1.`senttime` , t1.`readstatus`, t1.`responded`, t1.`merchanthidden`
FROM `inquiries` as t1
INNER JOIN users as fromData on t1.from_id = fromData.id
INNER JOIN users as toData on t1.to_id = toData.id
LEFT JOIN products as prodData on t1.product_id = prodData.id
WHERE t1.id=13
I'm having a hard time wrapping my head around how the column product_count works. It seems to be somehow referencing itself with aliases see and e, which are both pointing to catalog_category_entity. Specifically,
WHERE (see.entity_id = e.entity_id) OR (see.path LIKE CONCAT(e.path, '/%'))
Both see and e are aliases for table catalog_category_entity. What is this doing?
Here's the entire query:
SELECT `e`.*, `d_name`.`value` AS `name`, IF(s_name.value_id > 0, s_name.value, d_name.value) AS `name`, `d_is_active`.`value` AS `is_active`, IF(s_is_active.value_id > 0, s_is_active.value, d_is_active.value) AS `is_active`, `d_is_anchor`.`value` AS `is_anchor`, IF(s_is_anchor.value_id > 0, s_is_anchor.value, d_is_anchor.value) AS `is_anchor`,
(
SELECT COUNT(DISTINCT scp.product_id)
FROM `catalog_category_entity` AS `see`
LEFT JOIN `catalog_category_product` AS `scp`
ON see.entity_id=scp.category_id
WHERE (see.entity_id = e.entity_id) OR (see.path LIKE CONCAT(e.path, '/%'))
) AS `product_count`,
(
SELECT COUNT(cp.product_id)
FROM `catalog_category_product` AS `cp`
WHERE (cp.category_id = e.entity_id)
) AS `self_product_count`
FROM `catalog_category_entity` AS `e`
LEFT JOIN `catalog_category_entity_varchar` AS `d_name` ON d_name.entity_id=e.entity_id AND d_name.attribute_id=41 AND d_name.entity_type_id=e.entity_type_id AND d_name.store_id=0
LEFT JOIN `catalog_category_entity_varchar` AS `s_name` ON s_name.entity_id=e.entity_id AND s_name.attribute_id=41 AND s_name.entity_type_id=e.entity_type_id AND s_name.store_id=0
LEFT JOIN `catalog_category_entity_int` AS `d_is_active` ON d_is_active.entity_id=e.entity_id AND d_is_active.attribute_id=42 AND d_is_active.entity_type_id=e.entity_type_id AND d_is_active.store_id=0
LEFT JOIN `catalog_category_entity_int` AS `s_is_active` ON s_is_active.entity_id=e.entity_id AND s_is_active.attribute_id=42 AND s_is_active.entity_type_id=e.entity_type_id AND s_is_active.store_id=0
LEFT JOIN `catalog_category_entity_int` AS `d_is_anchor` ON d_is_anchor.entity_id=e.entity_id AND d_is_anchor.attribute_id=51 AND d_is_anchor.entity_type_id=e.entity_type_id AND d_is_anchor.store_id=0
LEFT JOIN `catalog_category_entity_int` AS `s_is_anchor` ON s_is_anchor.entity_id=e.entity_id AND s_is_anchor.attribute_id=51 AND s_is_anchor.entity_type_id=e.entity_type_id AND s_is_anchor.store_id=0
WHERE (`e`.`entity_type_id` = '3') AND (e.entity_id IN('24', '533')) ORDER BY LENGTH(e.path) ASC
This is an example of a correlated query, here the query owner is checking a condition from outside of the sub-query with an OR clause.
In the below sub-query there is a distinct count of production_id which should exist in both tables: catalog_category_entity and catalog_category_product (even here there is no requirement of left join and an inner join may work better as you get count from the right side table) with condition that either entitiy_id should exist in catalog_category_entity as main table OR sub-query path field should match with the main table left part of path field means the main table may contain an extra string in right side but the left part should be same.
SELECT COUNT(DISTINCT scp.product_id)
FROM `catalog_category_entity` AS `see`
LEFT JOIN `catalog_category_product` AS `scp`
ON see.entity_id=scp.category_id
WHERE (see.entity_id = e.entity_id)
OR (see.path LIKE CONCAT(e.path, '/%'))
You can simplify your query if the requirement is clear as you are joining table catalog_category_entity_int 4 times with left join, while you can use as below only a single time:
LEFT JOIN catalog_category_entity_int` AS `d_is_anchor`
ON d_is_anchor.entity_id=e.entity_id
AND d_is_anchor.attribute_id IN (42,51)
AND d_is_anchor.entity_type_id=e.entity_type_id
AND d_is_anchor.store_id=0
A bit of a newbie question, probably an INNER JOIN with an "AS" statement, but I can't figure it out...
This is for a MYSQL based competition app. I want to select the "img_title" for both img_id1 and img_id2. I can't figure out how to do it and still see which title is assigned to the associated _id1 or _id2.
My tables:
competitions
comp_id
img_id1
img_id2
on_deck
img_id
img_title
Desired results:
comp_id | img_id1 | img_title1 |img_id2 | img_title2
You need a join for each image:
SELECT comp.comp_id, img1.img_id, img1.img_title, img2.img_id, img2.img_title
FROM competitions comp
INNER JOIN on_deck img1 ON img1.img_id = comp.img_id1
INNER JOIN on_deck img2 ON img2.img_id = comp.img_id2
LEFT JOIN if img_id1 or img_id2 can be NULL.
select comp_id, img_id1, b.img_title as img_title1,
img_id2, b2.img_title as img_title2
from competitions a
left outer join on_deck b on b.img_id = a.img_id1
left outer join on_deck b2 on b2.img_id = a.img_id2
switch let outer join to inner join if you want to exclude rows in competitions that do not have two matching img_ids
This query should give you the results you want. This also assumes that comp.img_id1 and comp.img_id2 are never NULL.
SELECT comp.comp_id
, comp.img_id1
, deck1.img_title AS img_title1
, comp.img_id2
, deck2.img_title AS img_title2
FROM competitions AS comp
JOIN on_deck deck1 ON comp.img_id1 = deck1.img_id
JOIN on_deck deck2 ON comp.img_id2 = deck2.img_id
If you have plan on having a NULL or empty string comp.img_id1 and/or comp.img_id2 fields, you'll need to do some left joins.
I have this query:
SELECT DISTINCT
enc.id, enc.cus_id, enc.createdon, enc.status, enc.segment, enc_task.orderid,
enc_task.taskid, enc.currentstep, enc.groupid, enc.fdprotocol,
enc_task.linkfile, cus.fname, cus.lname, login.first_name, login.last_name,
login.username, login.user_code, login.id as assigned_id, fp.protocol,
init.typename, table4.allowtotal
FROM
mob_encounters enc
JOIN mob_encounters_task enc_task ON enc_task.encounterid=enc.id
JOIN mob_customer cus ON cus.id=enc_task.cus_id
JOIN mob_login login ON login.id=enc.createdby
LEFT JOIN mob_protocol_type fp ON fp.id=enc.fdprotocol
LEFT JOIN initiation_type init ON init.id=enc.groupid
LEFT JOIN mob_table4 table4 ON table4.encid=enc.id
GROUP BY
enc.id
The query is working, except I need it to return the latest row of enc_task.encounterid when doing this match: enc_task.encounterid = enc.id. Is it possible?
A subquery might do what you're looking for:
SELECT DISTINCT
enc.id, enc.cus_id, enc.createdon, enc.status, enc.segment, enc_task.orderid, enc_task.taskid,
enc.currentstep, enc.groupid, enc.fdprotocol, enc_task.linkfile, cus.fname,
cus.lname, login.first_name, login.last_name, login.username, login.user_code,
login.id as assigned_id, fp.protocol, init.typename, table4.allowtotal
FROM
mob_encounters enc
JOIN
(select encounterid, orderid, taskid, linkfile from mob_encounters_task order by [FIELD THAT DESIGNATES LATEST VALUE] desc) AS enc_task ON enc_task.encounterid=enc.id
JOIN
mob_customer cus ON cus.id=enc_task.cus_id
JOIN
mob_login login ON login.id=enc.createdby
LEFT JOIN
mob_protocol_type fp ON fp.id=enc.fdprotocol
LEFT JOIN
initiation_type init ON init.id=enc.groupid
LEFT JOIN
mob_table4 table4 ON table4.encid=enc.id
GROUP BY
enc.id