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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:38:01+00:00 2026-05-24T06:38:01+00:00

My problem is Play! Framework / JPA specific. But I think it’s applicable to

  • 0

My problem is Play! Framework / JPA specific. But I think it’s applicable to general SQL syntax.

Here is a sample query with a simple JOIN:

return Post.find(
    "select distinct p from Post p join p.tags as t where t.name = ?", tag
).fetch();

It’s simple and works well.

My question is: What if I want to JOIN on more values in the same table?

Example (Doesn’t work. It’s a pseudo-syntax I created):

return Post.find(
    "select distinct p from Post p join p.tags1 as t, p.tags2 as u, p.tags3 as v where t.name = ?, u.name = ?, v.name = ?", tag1, tag2, tag3,
).fetch();
  • 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-24T06:38:02+00:00Added an answer on May 24, 2026 at 6:38 am

    Your programming logic seems okay, but the SQL statement needs some work. Seems you’re new to SQL, and as you pointed out, you don’t seem to understand what a JOIN is.

    You’re trying to select data from 4 tables named POST, TAG1, TAG2, and TAG3.

    I don’t know what’s in these tables, and it’s hard to give sample SQL statements without that information. So, I’m going to make something up, just for the purposes of discussion. Let’s say that table POST has 6 columns, and there’s 8 rows of data in it.

    P Fname Lname   Country Color Headgear
    - ----- -----   ------- ----- --------
    1 Alex  Andrews 1       1     0
    2 Bob   Barker  2       3     0
    3 Chuck Conners 1       5     0
    4 Don   Duck    3       6     1
    5 Ed    Edwards 2       4     2
    6 Frank Farkle  4       2     1
    7 Geoff Good    1       1     0
    8 Hank  Howard  1       3     0
    

    We’ll say that TAG1, TAG2, and TAG3 are lookup tables, with only 2 columns each. Table TAG1 has 4 country codes:

    C Name
    - -------
    1 USA
    2 France
    3 Germany
    4 Spain
    

    Table TAG2 has 6 Color codes:

    C Name
    - ------
    1 Red
    2 Orange
    3 Yellow
    4 Green
    5 Blue
    6 Violet
    

    Table TAG3 has 4 Headgear codes:

    C Name
    - -------
    0 None
    1 Glasses
    2 Hat
    3 Monacle
    

    Now, when you select data from these 4 tables, for P=6, you’re trying to get something like this:

    Fname Lname  Country Color  Headgear
    ----- ------ ------- ------ -------
    Frank Farkle Spain   Orange None
    

    First thing, let’s look at your WHERE clause:

    where t.name = ?, u.name = ?, v.name = ?
    

    Sorry, but using commas like this is a syntax error. Normally you only want to find data where all 3 conditions are true; you do this by using AND:

    where t.name=? AND u.name=? AND v.name=?
    

    Second, why are you joining tables together? Because you need more information. Table POST says that Frank’s COUNTRY value is 4; table TAG1 says that 4 means Spain. So we need to “join” these tables together.

    The ancient (before 1980, I think) way to join tables is to list more than one table name in the FROM clause, separated by commas. This gives us:

    SELECT P.FNAME, P.LNAME, T.NAME As Country, U.NAME As Color, V.NAME As Headgear
    FROM POST P, TAG1 T, TAG2 U, TAG3 V
    

    The trouble with this query is that you’re not telling it WHICH rows you want, or how they relate to each other. So the database generates something called a “Cartesian Product”. It’s extremely rare that you want a Cartesian Product – normally this is a HUGE MISTAKE. Even though your database only has 22 rows in it, this SELECT statement is going to return 768 rows of data:

    Alex Andrews USA   Red    None
    Alex Andrews USA   Red    Glasses
    Alex Andrews USA   Red    Hat
    Alex Andrews USA   Red    Monacle
    Alex Andrews USA   Orange None
    Alex Andrews USA   Orange Glasses
    ...
    Hank Howard  Spain Violet Monacle
    

    That’s right, it returns every possible combination of data from the 4 tables. Imagine for a second that the POST table eventually grows to 20000 rows, and the three TAG tables have 100 rows each. The whole database would be less than a megabyte, but the Cartesian Product would have 20,000,000,000 rows of data — probably about 120 GB of data. Any database engine would choke on that.

    So if you want to use the Ancient way of specifying tables, it is VERY IMPORTANT to make sure that your WHERE clause shows the relationship between every table you’re querying. This makes a lot more sense:

    SELECT P.FNAME, P.LNAME, T.NAME As Country, U.NAME As Color, V.NAME As Headgear
    FROM POST P, TAG1 T, TAG2 U, TAG3 V
    WHERE P.Country=T.C AND P.Color=U.C AND P.Headgear=V.C
    

    This only returns 8 rows of data.

    Using the Ancient way, it’s easy to accidentally create Cartesian Products, which are almost always bad. So they revised SQL to make it harder to do. That’s the JOIN keyword. Now, when you specify additional tables you can specify how they relate at the same time. The New Way is:

    SELECT P.FNAME, P.LNAME, T.NAME As Country, U.NAME As Color, V.NAME As Headgear
    FROM POST P
    INNER JOIN TAG1 T ON P.Country=T.C
    INNER JOIN TAG2 U ON P.Color=U.C
    INNER JOIN TAG3 V ON P.Headgear=V.C
    

    You can still use a WHERE clause, too.

    SELECT P.FNAME, P.LNAME, T.NAME As Country, U.NAME As Color, V.NAME As Headgear
    FROM POST P
    INNER JOIN TAG1 T ON P.Country=T.C
    INNER JOIN TAG2 U ON P.Color=U.C
    INNER JOIN TAG3 V ON P.Headgear=V.C
    WHERE P.P=?
    

    If you call this and pass in the value 6, you get only one row back:

    Fname Lname  Country Color  Headgear
    ----- ------ ------- ------ --------
    Frank Farkle Spain   Orange None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am brand new to the Play framework, but I am trying to use
I have a problem with persisting data via play-framework. Maybe it's not possible to
I'm trying JPA with a very simple class for the Play! framework and I'm
I'm having a problem when trying to use layouts in the Play! Framework. As
I've just started playing with Play! framework, and stumbled on such problem: modules that
I'm working on a 1.2 Play! framework application, and I have a problem when
I'm using Play Framework and I have what I think is a very frequent
Im using the AVFoundation framework to play sound files. The problem im having is
While trying to learn Play Framework I ran across the following problem: How do
Im having a problem getting zend framework to play nicely with subdomains on my

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.