We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24969
    • 120 Posts
    Hello,
    I have a mysql database with a table tbldog...
    this table has many dogs listed in it.
    select * from `chrys-haefen`.`tbldogs` WHERE `litter` is not NULL LIMIT 0, 30 ;
    

    the preceeding statement products 4 rows.
    dog_id 	sex callname 	litter 
    76 	Male 	Rigger 	127
    85 	Female 	Laila 	129
    127 	Female 	Marley 	76
    129 	Male 	Ivan 	85
    

    I need to create a sql statement which will produce the combination Laila X Ivan... as Laila and Ivan were mated together. Similarly Marley X Rigger were also mated.

    Is there a solution here?
      The important thing is not to stop questioning. Curiosity has its own reason for existing. One cannot help but be in awe when he contemplates the mysteries of eternity, of life, of the marvelous structure of reality. It is enough if one tries merely to comprehend a little of this mystery every day. Never lose a holy curiosity.

      -Albert Einstein (1879 - 1955)

      Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
      • 3749
      • 24,544 Posts
      From what you’ve described, your database doesn’t hold any information about any dog’s mate(s). If that’s the case, no query will produce what you want unless you specify the names or IDs of the two dogs in the query.

      You would need another table of mates:

      M F
      129 185
      76 127

      You might want a few more fields here for litter number, mating date, etc.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        I’ve noticed that the litter number of the one mate matches the dog_id of the other mate. Is this a consistent match?

        A second table for breeding would be more appropriate, as BobRay mentioned; it could also have fields for date of mating, outcome, number of puppies, date of whelping and notes.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 11055 ☆ A M B ☆
          • 3,112 Posts
          I think rhyno talks about "making the query" rather than "compare the table".

          http://svn.modxcms.com/docs/display/MODx096/Plugins
            Rico
            Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
            MODx is great, but knowing how to use it well makes it perfect!

            www.virtudraft.com

            Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

            Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

            Maintainter/contributor of Babel

            Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
            • 4172
            • 5,888 Posts
            Try this-one, but it would really be better to have an own table for mates:
            SELECT male.callname as male_callname,female.callname as female_callname FROM `chrys-haefen`.`tbldogs` male
            left join `chrys-haefen`.`tbldogs` female on female.litter = male.dog_id
            where male.sex = 'male'
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 3749
              • 24,544 Posts
              I see what you’re doing now. I had assumed that the litter number was for the litter the dog born into (which you’ll probably want to know down the road).

              I don’t think this will work very well in the long run. You have two separate litter numbers for each litter and when both males and females have multiple litters (possibly with the same mate again) things will get pretty nasty.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Indeed; that’s why relational databases are called "relational" (because of the relationships between tables), and why there is the whole subject of "normalization".

                http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 24969
                  • 120 Posts
                  Thanks for the replys... I have come up with the following query which returns the following result set.
                  Query:
                  SELECT d.callname, d.regname, d.sex, i.filelocation, i.description, (
                  SELECT callname
                  FROM tbldogs d2
                  WHERE d2.litter = d.dog_id
                  ) AS mated
                  FROM tbldogs d, tblimages i
                  WHERE d.litter != '0'
                  AND d.breedID =87
                  AND i.dogfk = d.dog_id
                  AND use_pict = 'Y'
                  ORDER BY sex


                  Results:
                  callname 	regname 	sex 	filelocation 	description 	mated
                  Laila 	Chrys-haefen Floatlikeabutafly 	Female 	images/dogs/goldens/laila/layla-stackedsm.jpg 	Laila Stacked 	Ivan
                  Marley 	Chrys-haefen Don’t Rock T’Boat 	Female 	images/dogs/goldens/marley/marleystackedsm.jpg 	Marley Stacked 	Rigger
                  Rigger 	Chrys-haefen Rigger Folkins 	Male 	images/dogs/goldens/rigger/riggersm.jpg 	Rigger 	Marley
                  Ivan 	Guldakra's Ivan Lendl 	Male 	images/dogs/goldens/ivan/ivan-1-year-oldsm.jpg 	Ivan 	Laila


                  This is what I need for my client.... But I need to store the resulting query in parameters that relate to sire and dam....
                  Sire would be Rigger.
                  Dam would be Laila.
                  My snippet looks like this.
                  <?php
                  $output = '';//create variable for holding the output
                  $sql = $modx->db->query( 'SELECT d.callname, d.regname, i.filelocation, i.description, (
                  SELECT callname
                  FROM tbldogs d2
                  WHERE d2.litter = d.dog_id
                  ) AS mated
                  FROM tbldogs d, tblimages i
                  WHERE d.litter != '0'
                  AND d.breedID =87
                  AND i.dogfk = d.dog_id
                  AND use_pict = 'Y'
                  ORDER BY sex
                  LIMIT 0 , 1000');//define the DB query, in this case the table is named 'tbldogs'
                  $resultArray = $modx->db->makeArray($sql);//put it into an array
                  foreach($resultArray as $item) //go through each record in the array
                  {//start looping through the data array
                  $params['sire']=$item['regname'];//set the placeholders, the $params sets the name of the placeholder, the $item is the field name from the DB table
                  $params['sirecallname']=$item['callname'];
                  $params['dam']=$item['regname'];
                  $params['damcallname']=$item['callname'];
                  $params['photo']=$item['filelocation'];
                  $params['alt_text']=$item['description'];
                  $output.=$modx->parseChunk('Golden Litters', $params, '[+', '+]');//define the chunk name and process the content
                  }//finish looping through the data array
                  return $output;//now return the output from the processed chunk
                  ?>


                  How can I store the sire and dam parameters correctly??
                    The important thing is not to stop questioning. Curiosity has its own reason for existing. One cannot help but be in awe when he contemplates the mysteries of eternity, of life, of the marvelous structure of reality. It is enough if one tries merely to comprehend a little of this mystery every day. Never lose a holy curiosity.

                    -Albert Einstein (1879 - 1955)

                    Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
                    • 24969
                    • 120 Posts
                    After further thought on this issue, I’ve decided to create a separate table named tblbreeding. Having the following column in it.
                    id 	SireFK 	DamFK 	dom Date Of Breeding	dow Date of Whelping	rd Relse Date to new home	no_male_puppies Number of Males Born	no_female_puppies Number of Female Puppies born	breedFK BreedID of the litter
                    1 	129 	85 	NULL 	2009-08-06 	2009-10-01 	7 	3 	87
                    2 	76 	127 	NULL 	2009-08-05 	2009-09-30 	4 	2 	87


                    question:
                    Is there anyway of using the sireFK and the damFK fields to retrieve the information saved in tbldogs within modx. Preferably using 1 sql statement?
                      The important thing is not to stop questioning. Curiosity has its own reason for existing. One cannot help but be in awe when he contemplates the mysteries of eternity, of life, of the marvelous structure of reality. It is enough if one tries merely to comprehend a little of this mystery every day. Never lose a holy curiosity.

                      -Albert Einstein (1879 - 1955)

                      Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635