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

  • Home
  • SEARCH
  • 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 7442803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:13:54+00:00 2026-05-29T11:13:54+00:00

Why do i get this error with this query? SELECT distinct fileID FROM (

  • 0

Why do i get this error with this query?

SELECT distinct fileID 
FROM ( SELECT fileID from file order by fileID desc limit 30) as R1 
UNION (SELECT fileID from visit order by counter desc limit 30 ) 
WHERE status = 1 order by rand() LIMIT 10

error:

You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ‘WHERE status = 1 order by rand() LIMIT 10’ at line 1

what i want is to select the top 30 most viewed file and top 30 most recent file and then randomly select from them limit by 10 with the file status = 1.

  • 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-29T11:13:55+00:00Added an answer on May 29, 2026 at 11:13 am

    put your two queries for newest files and most visits in a UNION inside a subquery, and then reorder the resulting derived table by RAND and limit the result to 10.

    Note:

    you state you want to select recently visited files.. but ordering by “counter” in fact selects the files with the most visits .. not the recent ones.

    SELECT DISTINCT fileID 
    FROM (
      SELECT file.fileID 
        FROM file 
        WHERE file.status = 1 
        ORDER BY file.fileID DESC 
        LIMIT 30
      UNION ALL
      SELECT visit.fileID 
        FROM visit 
          JOIN file ON file.fileID = visit.fileID 
        WHERE file.status = 1 
        ORDER BY visit.counter DESC 
        LIMIT 30 
    ) dt
    ORDER BY RAND() 
    LIMIT 10
    

    As a reaction to the OPs comment, i added a JOIN in one of the selects to check if the file related to a visit has status = 1.

    Also Note:

    for this query to perform fast on larger amounts of data you should add index on the fields file.status and visit.counter. If fileID is not already a key / index for those tables, you should add an index on this field too.


    The visit.file_id is not UNIQUE so the above query may have the same file_ids in the sub-result of the 2nd subquery (the duplicates will be removed by the final DISTINCT but that means that the second LIMIT 30 does not work as wanted). Possible correction:

    SELECT DISTINCT fileID 
    FROM (
      SELECT file.fileID 
        FROM file 
        WHERE file.status = 1 
        ORDER BY file.fileID DESC 
        LIMIT 30
      UNION ALL
      SELECT visit.fileID 
        FROM visit 
          JOIN file ON file.fileID = visit.fileID 
        WHERE file.status = 1 
        GROUP BY visit.fileId
        ORDER BY MAX(visit.counter) DESC 
        LIMIT 30 
    ) dt
    ORDER BY RAND() 
    LIMIT 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my base query select distinct a.projects , case when(billing_fy!=0) then(select round(((sum(cost_project)/(sum(billing_fy)/((10/12)*365)))),2) from
I have this PHP code: $result = mysql_query(SELECT distinct om_quote_no from `porders` order by
I'm trying ro execute this dql query SELECT DISTINCT a FROM Ipf\Model\Entity\Transaction t INNER
I have this query and I get error Operand should contain 1 column(s), whats
I get this error message when installing SQL 2005 Analysis services. The cabinet File
I have the following query: SELECT wm_concat(DISTINCT NAME) as Methods FROM TPM_TRAININGPLAN JOIN TPM_DELIVERYMETHODS
I am trying to run this query on http://dbpedia.org/sparql but I get an error
I'm trying to get the following query to work in Hibernate: SELECT m FROM
Ok so here is the example query: SELECT DISTINCT id, amount FROM tbl_recurring_payment AS
This is my query: SELECT autor.entwickler,anwendung.name FROM autor left join anwendung on anwendung.name =

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.