Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Joined query

  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    367
    Thanks
    6
    Thanked 27 Times in 27 Posts

    Joined query

    Hi there

    Usually i do joined querys like this:
    Code:
    select tableA.id,tableB.oid,tableC.example as whatever from tableA,tableB,tableC where tableA.lala=tableC.something
    which actually works.
    But for my new projekt i (guess) have to do join by it's meaning. Because i need to join the same table with itself. I i have no idea how to achieve that.

    Again:

    My goal ist to get all friends (ID's of freinds = tid) of user A (uid) and check if there is some friends of tid which user A don't know of.

    So i have three tables.

    tableA (FRIENDS){
    id = unique id of entry
    what = type of friend (e.g. Spouse)
    uid = id of user A
    tid = id of user B
    }


    tableB (USER){
    id = unique id of entry (so of course the id of user /uid or tid)
    gender = (2/female, 1/male)
    }


    tableC (Already stored){
    id = unique id of entry
    fid = should store the unique entry id of tableA
    uid = id of user A friend stored in tableA FRIENDS as uid
    tid = unknown id of unknown friend should be known
    what = should store value of same field as what of tableA
    gender = should store value of same field as what of tableB
    seen = NUMBER 1 or 2 (1 NOT SEEN YET, 2 SEEN YET)
    fid = should store the unique id of tableA
    }

    GOAL:
    Figure out what friends do USER A have, and what kind of friend (what,id), what gender do they have and store it to tableC if there is no entry on tableC or field seen of tableC is 2 already (so query should left that out like seen<2 or seen !=2)

    Right now i do that with two querys. But my goal is one query.

    Is there a way?

    EDIT: Please no answers like check the documentation of this link or check this link. I've already checked a lot. There is NO example of sql join that explains how to query the same table again. And that is what i need. If you read my needs above.

    Because i want to left out results, which absolutely have nothing to do with User A (to save performance). Thats why i have to check if USER A is really a friend of tid, and than only check friends of tid (and those only who are not friends to user A already)
    Last edited by Labrar; Jul 24th, 2019 at 06:19 PM.
    I never ever read PM's unless it's an job offer. So save your time for regular questions in the forum. I never ever take friendship offers. We are not on facebook here.(It's stupid on facebook too). Friendship? Do i know you? Did we ever had a beer together? Thats really stupid. Sorry.

  2. #2
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,632
    Thanks
    11
    Thanked 164 Times in 159 Posts
    Stop joining in the WHERE. It is a major performance problem. Do table joins the way they are supposed to be done.

    https://dev.mysql.com/doc/refman/8.0/en/join.html
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio

  3. #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    367
    Thanks
    6
    Thanked 27 Times in 27 Posts
    In other words, i should do multiple seperated querys to achieve may goal?
    I never ever read PM's unless it's an job offer. So save your time for regular questions in the forum. I never ever take friendship offers. We are not on facebook here.(It's stupid on facebook too). Friendship? Do i know you? Did we ever had a beer together? Thats really stupid. Sorry.

  4. #4
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,632
    Thanks
    11
    Thanked 164 Times in 159 Posts
    NO, read my post and RTFM and learn how to do a join.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    Make A Donation https://www.paypal.me/KevinRubio


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •