I'm a mysql beginner these days, i appreciate for your advise.
I got a problem my sql query
INSERT IGNORE INTO TB_AUTO_BAN(MEMO, REG_DATE, USER_ID, NAME, PHONE_NUM)
(
SELECT 'test' AS MEMO, NOW() AS REG_DATE, a.USER_ID, a.NAME, a.CONTACT_NUM
FROM TB_CONTACT AS a,TB_CONTACT_GROUP AS b
WHERE b.USER_ID = 'spark#naver.com'
AND b.GROUP_CONTACT_SEQ = IN(12800,12801)
AND a.GROUP_CONTACT_SEQ = b.GROUP_CONTACT_SEQ
Bold text part is the problem, how should I modify it??
Your query looks ok, except for the = in. Only in is needed.
I would write the query as:
INSERT IGNORE INTO TB_AUTO_BAN(MEMO, REG_DATE, USER_ID, NAME, PHONE_NUM)
SELECT 'test' AS MEMO, NOW() AS REG_DATE, c.USER_ID, c.NAME, c.CONTACT_NUM
FROM TB_CONTACT AS c JOIN
TB_CONTACT_GROUP AS cg
ON c.GROUP_CONTACT_SEQ = cg.GROUP_CONTACT_SEQ
WHERE cg.USER_ID= 'spark#naver.com' AND
cg.GROUP_CONTACT_SEQ IN (12800, 12801) ;
In addition to fixing the condition, this changes the table aliases to be abbreviations rather than arbitrary letters. It also uses standard join syntax instead of implicit joins with the condition buried in the where clause.
the equality sign "=" is superfluous. just delete it, and leave: b.GROUP_CONTACT_SEQ IN (12800,12801)
Related
This form of my correlated sub query comes up with the error message "Unknown column 'Invoices.TranDate' in 'where clause'"
select InvoiceID, TranDate
, ifnull(TotPayments,0) TotPayments, ifnull(CountPayments,0) CountPayments
from Invoices
left join (select DebtorID, sum(TranAmount) TotPayments, count(*) CountPayments
from CashTrans
where CashTrans.TranDate >= Invoices.TranDate
group by DebtorID) PY on PY.DebtorID = Invoices.DebtorID
Yet this version works
select InvoiceID, TranDate
, (select sum(TranAmount) from CashTrans
where CashTrans.TranDate >= Invoices.TranDate
and CashTrans.DebtorID = Invoices.DebtorID) TotPayments
, (select count(*) from CashTrans
where CashTrans.TranDate >= Invoices.TranDate
and CashTrans.DebtorID = Invoices.DebtorID) CountPayments
from Invoices;
What is wrong with the first query? The only thing I can think of is that on my Windows system I have configured lower_case_table_names=2 as I want to preserve mixed case names. Perhaps that has something to do with the first query not seeing Invoice.TranDate in scope? MySQL Documentation and internet searches have not thrown any light on the matter.
https://dev.mysql.com/doc/refman/8.0/en/lateral-derived-tables.html says:
A derived table cannot normally refer to (depend on) columns of preceding tables in the same FROM clause. As of MySQL 8.0.14, a derived table may be defined as a lateral derived table to specify that such references are permitted.
In SQL:1999, the query becomes legal if the derived tables are preceded by the LATERAL keyword (which means “this derived table depends on previous tables on its left side”):
I have not tested it, but I believe your query could be written this way:
SELECT InvoiceID, TranDate,
IFNULL(TotPayments,0) AS TotPayments,
ifnull(CountPayments,0) AS CountPayments
FROM Invoices
LEFT JOIN LATERAL (
SELECT DebtorID,
SUM(TranAmount) AS TotPayments,
COUNT(*) AS CountPayments
FROM CashTrans
WHERE CashTrans.TranDate >= Invoices.TranDate
GROUP BY DebtorID
) AS PY ON PY.DebtorID = Invoices.DebtorID;
Also be aware this requires you to use at least MySQL 8.0.14.
I`m trying to delete a lot of data via select. This select work appropriate and returns in result 75k+ rows. I need to delete them, but when I try to delete it this error occurs
#1241 - Operand should contain 1 column(s). I'm using PHPMyAdmin.
DELETE FROM `crm_wsal_metadata`
WHERE `occurrence_id` = ANY
(SELECT *
FROM `crm_wsal_metadata`
WHERE `name` = `PostDate` AND `value` BETWEEN str_to_date('2018-12-26', '%Y-%m-%d') AND str_to_date('2020-05-31', '%Y-%m-%d')
GROUP BY `occurrence_id`)
Use
... SELECT `occurence_id` ...
instead of SELECT *. The group by clause forces you to use only grouped columns and aggregations, not star (perhaps unless some proprietary quirks I don't recommend to rely on).
I had found the answer and will try to write it step by step:
Why does this error happen?
In MySQL, you can't modify the same table which you use in the SELECT part.
This behavior is documented at http://dev.mysql.com/doc/refman/5.6/en/update.html
How to make such thing happen?
There are two ways:
Join the table to itself
UPDATE tbl AS a
INNER JOIN tbl AS b ON ....
SET a.col = b.col
Nest the subquery deeper into a from clause
UPDATE tbl SET col = (
SELECT ... FROM (SELECT.... FROM) AS x);
Personally, in my case the code looked like this:
DELETE FROM crm_wsal_metadata
WHERE occurrence_id = ANY (
SELECT occurrence_id FROM (
SELECT occurrence_id FROM crm_wsal_metadata WHERE name = "PostDate" AND value BETWEEN str_to_date('2018-12-26', '%Y-%m-%d') AND str_to_date('2020-05-31', '%Y-%m-%d') AS search) )
Sorry for such bad styling. Im new with it :)
Trying to run this query and it keeps on telling me ambiguous column name on VendorID need help
Select VendorID
, VendorName
, InvoiceNumber
, InvoiceDate
, InvoiceTotal
FROM Vendors
JOIN Invoices
ON Vendors.VendorID = Invoices.InvoiceID
Just qualify all your column names, and you will never have this problem again. I also think your ON conditions are wrong:
SELECT v.VendorID, v.VendorName, i.InvoiceNumber, i.InvoiceDate, i.InvoiceTotal
FROM Vendors v JOIN
Invoices i
ON v.VendorID = i.VendorID;
-----------------------^
For completeness, I will note that you can fix this particular problem with the USING clause. However, it is better just to write code defensively so queries don't generate errors.
Is it possible to have a query with two NOT IN on two subqueries:
SELECT u.feedbackid
FROM user_feedback u
WHERE u.feedbackid NOT IN ( SELECT feedbackid
FROM user_feedback_sent)
AND NOT IN (SELECT feedbackid
FROM user_feedback_received)
The query throws an error on the second NOT IN saying incorrect syntax.
You're missing the column name which shuold NOT be in the second subquery. This will probably works as you need:
SELECT u.feedbackid
FROM user_feedback u
WHERE u.feedbackid NOT IN (SELECT a.feedbackid
FROM user_feedback_sent a)
AND u.feedbackid NOT IN (SELECT b.feedbackid
FROM user_feedback_received b)
Identation its always a good practice to implement when writing SQL code.
Hope it helps
This code runs fine. Pay special attention to the 'AS commercial' subquery field. It works.
SELECT `Contacts`.`id`,
(
SELECT `team_members`.`id`
FROM team_members
INNER JOIN team_categories_team_members AS memcat
ON `team_members`.`id` = `memcat`.`team_member_id`
WHERE `memcat`.`team_category_id` =3
) AS commercial
FROM `oys001`.`team_members` AS `Contacts`
JOIN `oys001`.`brands_team_members` AS `BrandsTeamMember` ON (
`BrandsTeamMember`.`brand_id` =2
AND `BrandsTeamMember`.`team_member_id` = `Contacts`.`id` )
However, now I want to perform a condition on it, so I just add this:
WHERE commercial > 0
And it tells me the field does not exist... What's going wrong here?
Wrap your inital query in brackets
SELECT *
FROM (yourqueryhere) AS `v`
WHERE commercial > 0