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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:01:20+00:00 2026-06-04T14:01:20+00:00

I have that problem: I have a model with those 3 tables: Linha, Itinerario

  • 0

I have that problem:
I have a model with those 3 tables:
Linha, Itinerario and Rua
Into Itinerario i have the reference id to Linha table and one reference with id for Rua.
In my code i recieve two arguments about idRua and i must return all Linhas where i have Itinerario the references for Rua with both idRua… In this examples where i have idRua = 1 and idRua = 2.

SELECT l.codigo, l.linha, l.idEmpresa, l.idLinha 
FROM Linha l 
INNER JOIN Itinerario i1 ON i1.idLinha = l.idLinha 
INNER JOIN Itinerario i2 ON i2.idLinha = l.idLinha 
WHERE i1.ida = i2.ida and i1.idRua = 1 and i2.idRua = 2 
ORDER BY l.linha

The problem is that i get 2 inner join in that table Itinerario and the query gets to slow…
Is there somehow to optimize it?
Is there some “IN” operator with “AND” condition or something like this?
Im using SQLite.

  • 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-04T14:01:22+00:00Added an answer on June 4, 2026 at 2:01 pm

    There may be a way to eliminate the self-join.

    If I’m reading this correctly, you want idLinha’s from Itinerario that contain both idRua = 1 and idRua = 2 for the same ida. I’m noticing that this is just a filter condition, since everything in the select comes from linha.

    The following gets this condition:

    SELECT idlinha
    From itinerario
    GROUP BY idlinha, ida
    having max(case when idRua = 1 then 1 else 0 end) = 1 and
           max(case when idRua = 2 then 1 else 0 end) = 1
    

    Now, we can use this in an “in” or “join” clause, as in:

    SELECT l.codigo, l.linha, l.idEmpresa, l.idLinha 
    FROM Linha l
    where l.idlinha in (SELECT idlinha
                        From itinerario
                        GROUP BY idlinha
                        having max(case when idRua = 1 then 1 else 0 end) = 1 and
                               max(case when idRua = 2 then 1 else 0 end) = 1
                       )
    order by l.linha
    

    It is possible that the group by will be faster than a self-join.

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

Sidebar

Related Questions

Problem I have a User model, and the Volunteer model that inherits from the
I am working with a few legacy tables that have relationships, but those relationships
In a couple of scripts that I use I have problem that is intermittent.
In the end, I have decided that this isn't a problem that I particularly
I have a problem that I thought was easily solved, but is turning out
I have a problem that can basically be summirized as: Byte[] barr = new
I have this problem that drives me mad, so I am here to ask
I have a problem that I have not yet tested/compiled and wondering if it
I have a problem that comes from me following tutorials without really understanding what
I have a problem that I need to sort out in javascript I believe,

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.