I have the following MySQL query:
select c.company_id,c.company_name, pe.pe_relationship, opt.oo_type
from pub_entity pe
inner join company c
on c.company_id = pe.pe_company_id
inner join opt_out opt
on opt.oo_company_id=c.company_id
where pe.pe_pn_id in
(SELECT pn_id
FROM pub_notice
WHERE pn_company_id=2523);
and opt.oo_type not in
('image','iframe')
However, when I run the query, in opt.oo_type column I am still getting image and iframe results.
opt.oo_type is of type enum('image','iframe','other') Can anyone tell me why I am still getting those results?
You have a ; at the end of your first WHERE clause, which is cutting off your NOT IN clause. Move it to the end of the query:
select c.company_id,c.company_name, pe.pe_relationship, opt.oo_type
from pub_entity pe
inner join company c on c.company_id = pe.pe_company_id
inner join opt_out opt on opt.oo_company_id=c.company_id
where pe.pe_pn_id in (SELECT pn_id FROM pub_notice WHERE pn_company_id=2523)
and opt.oo_type not in ('image','iframe');
Related
Good afternoon, I’m trying to fulfill the request while writing an error. Error # 1066 does not quite understand how it can be fixed in my particular case. Perhaps the problem is that I connect to the table several times and need an alias.
SELECT `employees`.`name`, `employees`.`surname`, `employees`.`patronymic`,
`doc`.`name`, `doc`.`agreement`, `tank`.`name`,
`liquid`.`name`, `WorkPlan`.`description`
FROM `WorkPlan` , `employees` , `doc` , `tank` , `liquid`
LEFT JOIN `WorkPlan` ON `tank`.`id` = `WorkPlan`.`id_tank`
LEFT JOIN `WorkPlan` ON `liquid`.`id` = `WorkPlan`.`id_liquid`
LEFT JOIN `WorkPlan` ON `doc`.`id` = `WorkPlan`.`id_doc`
AND `WorkPlan`.`id_tank` = `tank`.`id`
AND `WorkPlan`.`id_liquid` = `liquid`.`id`
AND `WorkPlan`.`id_doc` = `doc`.`id`
I suspect (by your SELECT list of columns) that you want to join employees to the other tables.
For every join you must specify in the ON clause the columns that relate the 2 tables and it is a good practice to use aliases for the tables which shorten the code and make it more readable:
SELECT e.name, e.surname, e.patronymic,
d.name, d.agreement,
t.name,
l.name,
w.description
FROM employees e
LEFT JOIN WorkPlan w ON e.? = w.?
LEFT JOIN tank t ON t.id = w.id_tank
LEFT JOIN liquid l ON l.id = w.id_liquid
LEFT JOIN doc d ON d.id = w.id_doc
Replace the ? with the names of the columns that relate employees with WorkPlan.
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've been trying to write some code in SQL, but it keeps coming up with a syntax error regarding the join, and I can't work out why.
SELECT `COUNTRY$`.country_name, `PARTNER$`.partner_name, count(member_id)
FROM `Member$`
Left Join `COUNTRY$`
ON `MEMBER$`.country_id=`COUNTRY$`.country_id
lEFT jOIN `PARTNER$`
on `MEMBER$`.partner_ID = `PARTNER$`.partner_ID
Group By country_name,Partner_name
Any help would be appreciated.
May have something to do with how your table names are in 'thisFormat$'. Also you did not specify which table member_id was coming and group by also doesn't specify which table country_name, partner_name was from.
Try putting aliases on the tablenames and see if that eliminates the problem
SELECT c.country_name, p.partner_name, count(m.member_id)
FROM `Member$` m
left join `COUNTRY$` c on c.country_id = m.country_id
left join `PARTNER$` p on p.partner_id = m.partner_id
GROUP BY c.country_name, p.partner_name
All I'd like to know is how many records there are in my query results but MySQL keeps kicking me off saying I have lost my connection. The query itself runs in about a second.
SELECT COUNT(*) FROM
(SELECT my208.eid AS contact, name AS the_status, cid208.lastmod AS status_date, boo208.boo_medium
FROM the_emails.my208
LEFT JOIN the_emails.cid208 ON cid208.eid = my208.eid
LEFT JOIN the_emails.boo208 ON boo208.eid = my208.eid
LEFT JOIN the_config.classes ON boo208.class_id = classes.id) foo
Why is this taking so long and is there a better way?
Include the count(*) in your inner query itself. See the modified query below.
Give it a try
SELECT my208.eid AS contact,
name AS the_status,
cid208.lastmod AS status_date,
boo208.boo_medium,
COUNT(*) as total_records
FROM the_emails.my208
LEFT JOIN the_emails.cid208 ON cid208.eid = my208.eid
LEFT JOIN the_emails.boo208 ON boo208.eid = my208.eid
LEFT JOIN the_config.classes ON boo208.class_id = classes.id
I have two queries that result two result sets i need to compare both the result sets and need to display the differences between them.Hope i will get good support.Thank you.These are my queries
Query:1
SELECT distinct c.sid_ident,c.fix_ident from corept.std_sid_leg as c INNER JOIN (SELECT sid_ident, transition_ident, max(sequence_num) seq, route_type FROM corept.std_sid_leg WHERE data_supplier='J' AND airport_ident='KBOS' GROUP BY sid_ident,transition_ident) b ON c.sequence_num=b.seq and c.sid_ident = b.sid_ident and c.transition_ident =b.transition_ident WHERE c.data_supplier='J' and c.airport_ident='KBOS';
Query:2
SELECT name,trans FROM skyplan_deploy.deploy_sids ON d.name=c.sid_ident WHERE apt = 'KBOS' AND name != trans;
Comparison is to be done on fields sid_ident in corept.std_sid_leg and name in skplan_deplay.deploy_sids. As Mysql does not support full outer join,I thought of using left join and right join and combine both the results.But i stuck up with this.Please help.I am getting syntax error while using left and right join.Thank you.
The following query should simulate a FULL OUTER JOIN in MySQL.
SELECT *
FROM A
LEFT OUTER JOIN B
ON A.NAME = B.NAME
WHERE B.ID IS NULL
UNION ALL
SELECT *
FROM B
LEFT OUTER JOIN A
ON B.NAME = A.NAME
WHERE A.ID IS NULL;
Compare the results of the with an actual FULL OUTER JOIN in SQL Server and you'll see it works.