Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:24:23+00:00 2026-06-14T19:24:23+00:00

I have a simple MySQL query that I want to convert to PostgreSQL. After

  • 0

I have a simple MySQL query that I want to convert to PostgreSQL. After 3 days I finally quit as I don’t understand what wrong here:

UPDATE webUsers u, 
(SELECT IFNULL(count(s.id),0) AS id, p.associatedUserId FROM pool_worker p 
LEFT JOIN shares s ON p.username=s.username 
WHERE s.our_result='Y' GROUP BY p.associatedUserId) a
SET shares_this_round = a.id WHERE u.id = a.associatedUserId

I have tried to convert it but it says error on SET. Here is my query:

UPDATE webusers 
SET (shares_this_round) = (a.id)
FROM (SELECT coalesce(count(s.id),0) AS id, p.associatedUserId FROM pool_worker p 
LEFT JOIN shares s ON p.username=s.username WHERE s.our_result='Y' GROUP BY p.associatedUserId) a, webusers w WHERE u.id = a.associatedUserId

Can anyone please tell me what’s wrong with it? I can’t sleep just because of this.

     ------------------------------EDIT-------------------------------------

shares table

CREATE TABLE shares (
id bigint NOT NULL,
rem_host character varying(255) NOT NULL,
username character varying(120) NOT NULL,
our_result character(255) NOT NULL,
upstream_result character(255),
reason character varying(50),
solution character varying(1000) NOT NULL,
"time" timestamp without time zone DEFAULT now() NOT NULL
);

webusers table

CREATE TABLE webusers (
id integer NOT NULL,
admin integer NOT NULL,
username character varying(40) NOT NULL,
pass character varying(255) NOT NULL,
email character varying(255) NOT NULL,
"emailAuthPin" character varying(10) NOT NULL,
secret character varying(10) NOT NULL,
"loggedIp" character varying(255) NOT NULL,
"sessionTimeoutStamp" integer NOT NULL,
"accountLocked" integer NOT NULL,
"accountFailedAttempts" integer NOT NULL,
pin character varying(255) NOT NULL,
share_count integer DEFAULT 0 NOT NULL,
stale_share_count integer DEFAULT 0 NOT NULL,
shares_this_round integer DEFAULT 0 NOT NULL,
api_key character varying(255),
"activeEmail" integer,
donate_percent character varying(11) DEFAULT '1'::character varying,
btc_lock character(255) DEFAULT '0'::bpchar NOT NULL
);

pool_workes table

CREATE TABLE pool_worker (
id integer NOT NULL,
"associatedUserId" integer NOT NULL,
username character(50),
password character(255),
allowed_hosts text
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:24:25+00:00Added an answer on June 14, 2026 at 7:24 pm

    First, I formatted to arrive at this less confusing but still incorrect query:

    UPDATE webusers 
    SET   (shares_this_round) = (a.id)
    FROM  (
       SELECT coalesce(count(s.id),0) AS id, p.associatedUserId
       FROM   pool_worker p 
       LEFT   JOIN shares s ON p.username=s.username
       WHERE  s.our_result='Y'
       GROUP  BY p.associatedUserId) a
       , webusers w
    WHERE u.id = a.associatedUserId
    

    There are multiple distinct errors and more sub-optimal parts in this statement. Errors come first and with bold emphasis. The last few items are just recommendations.

    1. Missing alias u for webuser. A trivial mistake.

    2. Missing join between w and a. Results in a cross join, which hardly makes any sense and is a very expensive mistake as far as performance is concerned. It is also completely uncalled for, you can drop the redundant second instance of webuser from the query.

    3. SET (shares_this_round) = (a.id) is a syntax error. You cannot wrap a column name in the SET clause in parenthesis. It would be pointless anyway, just like the parenthesis around a.id. The latter isn’t a syntax error, though.

    4. As it turns out after comments and question update, you created the table with double-quoted "CamelCase" identifiers (which I advise not to use, ever, for exactly the kind of problems we just ran into). Read the chapter Identifiers and Key Words in the manual to understand what went wrong. In short: non-standard identifiers (with upper-case letters or reserved words, ..) have to be double-quoted at all times.
      I amended the query below to fit the new information.

    5. The aggregate function count() never returns NULL by definition. COALESCE is pointless in this context. I quote the manual on aggregate functions:

      It should be noted that except for count, these functions return a
      null value when no rows are selected.

      Emphasis mine. The count itself works, because NULL values are not counted, so you actually get 0 where no s.id is found.

    6. I also use a different column alias (id_ct), because id for the count is just misleading.

    7. WHERE s.our_result = 'Y' … if our_result is of type boolean, like it seems it should be, you can simplify to just WHERE s.our_result. I am guessing here, because you did not provide the necessary table definition.

    8. It is almost always a good idea to avoid UPDATEs that do not actually change anything (rare exceptions apply). I added a second WHERE clause to eliminate those:

      AND   w.shares_this_round IS DISTINCT FROM a.id
      

      If shares_this_round is defined NOT NULL, you can use <> instead because id_ct cannot be NULL. (Again, missing info in question.)

    9. USING(username) is just a notational shortcut that can be used here.

    Put everything together to arrive at this correct form:

    UPDATE webusers w
    SET    shares_this_round = a.id_ct
    FROM  (
       SELECT p."associatedUserId", count(s.id) AS id_ct
       FROM   pool_worker p 
       LEFT   JOIN shares s USING (username)
       WHERE  s.our_result = 'Y'                        -- boolean?
       GROUP  BY p."associatedUserId"
       ) a
    WHERE w.id = a."associatedUserId"
    AND   w.shares_this_round IS DISTINCT FROM a.id_ct  -- avoid empty updates
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple query (in a mySQL view) that php is using to
I have a MySQL query that I'd like help optimizing. Its rather simple in
I have a little problem that I don't understand. I have a db that
Thanks in advance! I have a simple mysql and php blog that I built
I have a MySQL query that fetches a numeric value VARCHAR (20) from a
I have a PHP MySQL query that inserts some data into a MySQL database
Have a simple page that pulls results from MySQL and displays them in a
I have a very simple search query that looks like this select * from
I have a simple MySQL table thats contains a list of categories, level is
I have a simple MySQL table for storing notification in a web with social

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.