Please forgive the lack of formatting!!!
For some reason I can't figure out how to make it show up
pretty like everyone elses...
Long time roamer, first time poster. I'm a newb to MySql and I'm having trouble understanding why the following code doesn't work.
UPDATE CUSTOMER_T
SET Cust_Status = 'INACTIVE'
WHERE (SELECT DISTINCT Cust_No, Cust_Last_Name, Cust_Status
FROM CUSTOMER_T NATURAL JOIN BILLING_T
WHERE BILLING_T.Cust_No = BILLING_T.Cust_No
AND (DATEDIFF(BILLING_T.Billing_Due_Date, BILLING_T.Billing_Date_Paid) < (-14)
AND CUSTOMER_T.Cust_Status = "ACTIVE")
OR (CUSTOMER_T.Cust_Status = "ACTIVE"
AND BILLING_T.Billing_Date_Paid IS NULL));
What I'm trying to do is find anyone whose status is ACTIVE AND their bill is more than 14 days late
OR they are ACTIVE AND they have a NULL status for their Billing_Date_Paid field.
Then when I find them, I want to UPDATE their Cust_Status to be "INACTIVE".
If I simply run the following:
SELECT DISTINCT Cust_No, Cust_Last_Name, Cust_Status
FROM CUSTOMER_T NATURAL JOIN BILLING_T
WHERE BILLING_T.Cust_No = BILLING_T.Cust_No
AND (DATEDIFF(BILLING_T.Billing_Due_Date, BILLING_T.Billing_Date_Paid) < (-14)
AND CUSTOMER_T.Cust_Status = "ACTIVE")
OR (CUSTOMER_T.Cust_Status = "ACTIVE"
AND BILLING_T.Billing_Date_Paid IS NULL);
Then I can pinpoint the people I'm looking for. However, whenever I try to incorporate this code into an UPDATE statment (the first bit of code I posted) I get the following error:
Error Code: 1241. Operand should contain 1 column(s)
You can actually do a JOIN in the update like so:
UPDATE CUSTOMER_T a
INNER JOIN BILLING_T b ON
a.Cust_No = b.Cust_No AND
a.Cust_Status = 'ACTIVE' AND
(
b.Billing_Date_Paid IS NULL OR
DATEDIFF(b.Billing_Due_Date, b.Billing_Date_Paid) < -14
)
SET a.Cust_Status = 'INACTIVE'
Related
I am currently doing some SQL magic and wanted to update the stock in my companies ERP program. However if I try to run the following query I get the error mentioned in the title.
update llx_product lp
set stock = (select sum(ps.reel)
from llx_product_stock as ps, llx_entrepot as w
where w.entity IN (1)
and w.rowid = ps.fk_entrepot
and ps.fk_product = lp.rowid
group by ps.rowid)
The subquery by itself returns just one row if used with a rowid for the product.
select sum(ps.reel)
from llx_product_stock as ps, llx_entrepot as w
where w.entity in (1)
and w.rowid = ps.fk_entrepot
and ps.fk_product = 7372
group by ps.rowid
Any help would be appreciated
I would suggest writing the query as:
update llx_product lp
set stock = (select sum(ps.reel)
from llx_product_stock ps join
llx_entrepot w
on ps.fk_product = lp.rowid
where w.entity in (1) and
w.rowid = ps.fk_entrepot
);
An aggregation query with no group by cannot return more than one row. It is unclear how your version is returning more than one row because the key used in the group by also has an equality comparison. Perhaps there is some type conversion issue at play.
But in any case, without the group by, you cannot get the error you are currently getting.
For anyone wondering the solution to my issue was a very simple query, Gordon pointed in the right direction and I made it harder than it should be.
update llx_product lp
left join llx_product_stock ps on lp.rowid = ps.fk_product
set lp.stock = ps.reel
I am trying to check if the current user is already following the selected user, and I am doing this like so:
(I know it's not the best way, but as I am new to MYSQL this is as much as I have been able to come up with)
SELECT EXISTS (SELECT 1 FROM Activity WHERE IdOtherUser = 86 AND id = 145)
I am '145' and the user I selected is '86'.
Now that return '0' If I am not following and '1' If I am following that person.
Seems to be working already but it definetly needs improving!
Now what I would like to do is count the followers in the same query.
So count the people I am following and the people following me.
'Activity' is the table where I store the followers and I save them like this:
'id' = me
'idOtherUser' = other user I followed
'type' = type of action "follow"
I have done count's before when calculating the like counts, but I just cannot get my head around this!!
If anyone could spare some time to help me it is much appreciated!
I am sorry if the question is not the best, but I am still learning and trying my best to format them as clear as possible to understand.
Thanks in advance!!
If you trying to count the followers from specific id from table Activity you might do this way:
SELECT COUNT(idOtherUser) AS "I Follow",
(SELECT COUNT(idOtherUser) FROM Activity WHERE idOtherUser = 145 AND type = "follow"
) AS "FOLLOW ME",
(SELECT COALESCE(id,0) FROM Activity WHERE IdOtherUser = 86 AND id = 145 AND type = "follow")
FROM Activity WHERE id = 145 AND type = "follow"
you can use a "correlated subquery" to simplify the query and you might want distinct in the count also (depends on you data). I would avoid using spaces in column aliases too.
SELECT
COUNT(DISTINCT A1.idOtherUser) as i_follow
, (
SELECT
COUNT(DISTINCT A2.id)
FROM Activity A2
WHERE A2.idOtherUser = A1.id
AND A2.type = 'follow'
) as following_me
FROM Activity A1
WHERE A1.id = 145
AND A1.idOtherUser = 86
AND A1.type = 'follow'
Try it with distinct then without, if the result is the same leave distinct out of the query.
I was running this query for the last 6 months with no problems on several servers, but today got an error?
error_message: Database query failed: Column 'account_id' in field list is ambiguous
Column account_id is ambiguous?
$sql_query = "SELECT pricenotifier_criteria.criteria_id , pricenotifier_criteria.`event_id` , pricenotifier_criteria.`ticket_id` , pricenotifier_criteria.`criteria` ,pricenotifier_criteria.account_id ,seller_ids.user_id,seller_ids.seller_id
FROM pricenotifier_criteria
INNER JOIN seller_ids
ON ( pricenotifier_criteria.account_id = seller_ids.account_id )
INNER JOIN pricenotifier_users
ON (pricenotifier_criteria.user_id = pricenotifier_users.user_id )
INNER JOIN pricenotifier_pricing_table
ON ( pricenotifier_users.pricing_id = pricenotifier_pricing_table.pricing_id )
WHERE status=1 AND pricenotifier_pricing_table.processing_time = 15
AND pricenotifier_criteria.user_id = 339
ORDER BY pricenotifier_criteria.event_id
LIMIT 2000
OFFSET 0";
I have tried to run this query in sql in phpmyadmin but no error came there ? if i run this via php than i got that error
APOLOGY TO BE MADE
I am really sorry in the page where i was running this query i had called another page where other programmer didnt mentioned table prefrix that's why we got this error but all of the guys who replied was right so everyone has almost true answer about (why ambigius error comes). I posted it here because i was sure my query has nothing ambigious and i was right but the other file was running some query and had that error.
Cleaner version of the same thing...
SELECT c.criteria_id
, c.event_id
, c.ticket_id
, c.criteria
, c.account_id
, s.user_id
, s.seller_id
FROM pricenotifier_criteria c
JOIN seller_ids s
ON s.account_id = c.account_id
JOIN pricenotifier_users u
ON u.user_id = c.user_id
JOIN pricenotifier_pricing_table g
ON g.pricing_id = u.pricing_id
WHERE status = 1
AND g.processing_time = 15
AND c.user_id = 339
ORDER
BY c.event_id
LIMIT 2000;
I bet your php code just selects account_id without table prefix pricenotifier_criteria or seller_ids. Since both tables contain a field account_id this would be ambiguous without table prefixes.
This error :
error_message: Database query failed: Column 'account_id' in field list is ambiguous
means that account_id is the field of 2 or more tables. As an example :
SELECT account_id, other_things
FROM table_account
INNER JOIN table_account2 ON (table_account.account_id = table_account2.account_id)
account_id is the field of 2 tables : table_account and table_account2. Then, this is ambiguous, because from which table will the account_id supposed to be selected ?
As #strawberry states, the only field that can be ambiguous is status in the query you showed us. If you have really this problem, make sure you prefix all your fields in your query like this : table.field_name. Then, this will avoid this kind of error.
I've got a select statement that returns a list of all items in a document store that have comments (stored in a separate comments table).
What I'm trying to do is to update the value of another column in public_document_store (skin_id) for all the documents that have released comments, based on the statement below.
This returns the records I want to update:
SELECT public_document_store_talkback.document_id,
public_document_store.section_id
FROM public_document_store
INNER JOIN public_document_store_talkback ON public_document_store_talkback.document_id = public_document_store.document_id
WHERE public_document_store_talkback.is_released = 1
AND public_document_store_talkback.is_rejected = 0
AND public_document_store.section_id = 10;
I've tried to update the skin_id field like this:
Update public_document_store SET skin_id = 6
WHERE document_id IN (Select... [as per the statement above] )
But this returns an error:
[Err] 1241 - Operand should contain 1 column(s)
I've tried various other permutations based on other answers here, but without any luck (My knowledge of SQL is pretty basic, so apologies if I am missing something obvious here)
Any ideas how I can make this work would be much appreciated.
Your SELECT query needs only a little modification to convert it into an UPDATE statement,
UPDATE public_document_store a
INNER JOIN public_document_store_talkback b
ON b.document_id = a.document_id
SET a.skin_id = 6
WHERE b.is_released = 1 AND
b.is_rejected = 0 AND
a.section_id = 10
I've been trying to write a few little plugins for personal use with WHMCS. Essentially what I'm trying to do here is grab a bunch of information about a certain order(s), and return it as an array in Perl.
The Perl bit I'm fine with, it's the MySQL query I've formed that's giving me stress..
I know it's big and messy, but what I have is:
SELECT tblhosting.id, tblhosting.userid, tblhosting.orderid, tblhosting.packageid, tblhosting.server, tblhosting.domain, tblhosting.username, tblorders.invoiceid, tblproducts.gid, tblservers.ipaddress, tblinvoices.status
FROM tblhosting, tblproducts, tblorders, tblinvoices, tblservers
WHERE tblorders.status = 'Pending'
AND tblproducts.gid = '2'
AND tblservers.id = tblhosting.server
AND tblorders.id = tblhosting.orderid
AND tblinvoices.id = tblorders.invoiceid
AND tblinvoices.status = 'Paid'
I don't know if this /should/ work, but I assume I'm on the right track as it does return what I'm looking for, however it returns everything twice.
For example, I created a new account with the domain 'sunshineee.info', and then in PHPMyAdmin ran the above query.
id userid orderid packageid server domain username invoiceid gid ipaddress status
13 7 17 6 1 sunshineee.info sunshine 293 2 184.22.145.196 Paid
13 7 17 6 1 sunshineee.info sunshine 293 2 184.22.145.196 Paid
Could anyone give me a heads up on where I've gone wrong with this one.. Obvioiusly (maybe not obviously enough) I want this as only one row returned per match.. I've tried it with >1 domain in the database and it returned duplicates for each of the matches..
Any help would be much appreciated
:)
SELECT distinct tblhosting.id, tblhosting.userid, tblhosting.orderid, tblhosting.packageid, tblhosting.server, tblhosting.domain, tblhosting.username, tblorders.invoiceid, tblproducts.gid, tblservers.ipaddress, tblinvoices.status
FROM tblhosting, tblproducts, tblorders, tblinvoices, tblservers
WHERE tblorders.status = 'Pending'
AND tblproducts.gid = '2'
AND tblservers.id = tblhosting.server
AND tblorders.id = tblhosting.orderid
AND tblinvoices.id = tblorders.invoiceid
AND tblinvoices.status = 'Paid'
Well, its near impossible without any table definitions, but you are doing a lot of joins there. You are starting with tblhosting.id and working your way 'up' from there. If any of the connected tables has a double entry, you'll get more hits
You could add a DISTINCT to your query, but that would not fix the underlying issue. It could be a problem with your data: do you have 2 invoices? Maybe you should select everything (SELECT * FROM) and check what is returned, maybe check your tables for double content.
Using DISTINCT is most of the time not a good choice: it means either your query or your data is incorrect (or you don't understand them thoroughly). It might get you the right result for now, but can get you in trouble later.
A guess about the reason this happens:
You do not connect the products table to the chain of id's. So you are basically adding a '2' to your result as far as I can see. You join on products, and the only thing that limits that table is that "gid" should be 2. So if you add a product with gid 2 you get another result. Either join it (maybe tblproduct.orderid = tblorders.id ? just guessing here) or just remove it, as it does nothing as far as I can see.
If you want to make your query a bit clearer, try not implicitly joining, but do it like this. So you can actually see what's happening
SELECT tblhosting.id, tblhosting.userid, tblhosting.orderid, tblhosting.packageid, tblhosting.server, tblhosting.domain, tblhosting.username, tblorders.invoiceid, tblproducts.gid, tblservers.ipaddress, tblinvoices.status
FROM tblhosting
JOIN tblproducts ON /*you're missing something here!*/
JOIN tblorders ON tblorders.id = tblhosting.orderid
JOIN tblinvoices ON tblinvoices.id = tblorders.invoiceid
JOIN tblservers ON tblservers.id = tblhosting.server
WHERE
tblorders.status = 'Pending'
AND tblproducts.gid = '2'
AND tblinvoices.status = 'Paid'
I don't see in your query JOIN to tblproducts, it seems to be a reason.