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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:54:29+00:00 2026-06-06T23:54:29+00:00

I have two tables in my database: (1) PHRASES: t_phrase ======== I like They

  • 0

I have two tables in my database:

(1) PHRASES:

t_phrase
========
I like
They prefer
...
Somebody else wants

and

(2) PLACES:

n_id   t_place
====   =======
1      London
2      Paris
...
N      New York

Table PHRASES has at least as many rows as PLACES. I need to join these two tables in such a way as to select all places with one phrase for each of them – but phrases need to be randomly distributed across places. The overall places table isn’t too big: maybe, about 3-4 thousand rows, however there will be an additional WHERE clause on it that will limit the output to about 200 places at most.

Ideally, I’d like this to be in one SQL statement, but so far I haven’t been able to get my head around this. Therefore the second option is a stored function returning a row of (int, varchar, varchar). For this, I was thinking of something along the lines of:

  1. select all phrases in random order into an array of varchar
  2. loop over places taking one at a time and returning it along with the next phrase from the array

Somehow this seems to me very inefficient, but I can’t come up with anything better.

Can you suggest any better idea? Or, even better, one statement SQL, maybe?

Thanks in advance.

EDIT: Please note that the phrases should NOT be repeated in the resultset. There are always at least as many phrases as there are places.

  • 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-06T23:54:30+00:00Added an answer on June 6, 2026 at 11:54 pm

    I think the following will work:

    select (select phrase from phrases order by random() limit 1),
           place
    from places
    

    The select within the select should be called for each row, so it should return a different value each time.

    If you want just a random arrangement of the phrases and places, you can use windows functions:

    select ph.phrase, p.place
    from (select place, row_number() over (order by place) as seqnum
          from places p
         ) p join
         (select phrase, row_number() over (order by random()) as seqnum
          from phrases
         ) ph 
         on p.seqnum = ph.seqnum
    

    This orders the places by place (or any field could do). It randomizes the phrases, and joins on the resulting row numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database that look like that: Customer: C_ID city
I have two tables in my database, table1 and table2. They are identical. But
I have two tables in my database, and I would like to retrieve information
I have two tables in a database that looks like this members table1 id
I have two tables in my database that look like this CREATE TABLE IF
I have two database tables: "places" and "translations". The translations of places names are
I have two tables in my database who looks like this: User: userid(int) username(varchar)
I have two database tables with the following structure: actions: action_id int(11) primary key
I have two tables in my database schema that represent an entity having a
I have two tables in my database. Comments CommentsID MembersID CommentsBy CommentsDesc CommentsActive CommentsDateEntered

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.