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 7030371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:39:43+00:00 2026-05-28T00:39:43+00:00

Im having trouble with a sql join involving a union. I’m trying to pull

  • 0

Im having trouble with a sql join involving a union. I’m trying to pull a COUNT and a field from 2 tables but getting an error.

The query:

$sql_result7 = mysql_query("(SELECT COUNT (*) as alertcount, date as alertdate FROM alerts WHERE to_id='$id' AND date > '$lastcheck') UNION (SELECT COUNT (*) as mailcount, date maildate FROM mobmail WHERE to_id='$id' AND to_del=0 AND seen = '0')", $db); 

$rs7 = mysql_fetch_array($sql_result7);
$alerts = $rs7[alertcount]; 
$mails = $rs7[mailcount]; 
$last_alert = $rs7[alertdate]; 
$last_mail = $rs7[maildate];

Is it something to do with the date as alertdate part?

The error im getting is:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

  • 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-05-28T00:39:44+00:00Added an answer on May 28, 2026 at 12:39 am

    Besides the space between COUNT and (*), there is another issue. You can’t use $rs7[mailcount] nor $rs7[maildate] in your PHP code because your query is equivalent to:

    SELECT 
          COUNT(*) as alertcount
        , date as alertdate 
    FROM alerts 
    WHERE to_id = '$id' 
      AND date > '$lastcheck' 
    UNION 
    SELECT 
          COUNT(*)                      --- No "as mailcount" here
        , date                          --- No "as maildate" either 
    FROM mobmail 
    WHERE to_id = '$id' 
      AND to_del = 0 
      AND seen = '0'
    

    and will return two rows and only 2 columns:

    alertcount | alertdate
    -----------|------------
     24        | 2012-01-04
     73        | 2011-11-11
    

    Two ways to solve this problem:

    Either keep the query (changing the UNION to UNION ALL to ensure that you always get 2 rows) and chnage the PHP to use the 2 rows.

    Or change the query to:

    SELECT alertcount, alertdate, mailcount, maildate 
    FROM
          ( SELECT 
                  COUNT(*) AS alertcount
                , date     AS alertdate 
            FROM alerts 
            WHERE to_id = '$id' 
              AND date > '$lastcheck' 
          ) AS a 
      CROSS JOIN
          ( SELECT 
                  COUNT(*) AS mailcount
                , date     AS maildate 
            FROM mobmail 
            WHERE to_id = '$id' 
              AND to_del = 0 
              AND seen = '0'
          ) AS b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to join tables to get a count on a field but am
I'm having trouble getting the following to work in SQL Server 2k, but it
I'm trying to parse SQL search conditions and having trouble getting the parser to
I'm having trouble getting in touch with SQL Server Managemen Studio 2008! I want
I'm having trouble converting the following SQL-statement to LINQ-to-entities: SELECT l.* FROM locations l
I'm having trouble with a LINQ to SQL query getting the min value using
I'm having trouble trying to optimize the following query for sql server 2005. Does
I am having trouble with a SQL query. SELECT t.topicname, m. *, ms.avatar FROM
I'm having trouble getting my head around converting a traditional SQL aggregate query into
I'm having trouble getting the SQL for a report I need to generate. I've

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.