We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10226
    • 412 Posts
    I am upgrading my reptile keeping site. We will soon outgrow what jot and ditto can do. I have everything figured out... so I thought.

    let me give you a run down of the tables.

    master_ev - holds the event_type, animal_id and date
    animals - holds data about the animals, fairly static info
    ev_ref - holds event_type and a human readable text
    species - similar to above
    foodinv - hold type, weight and qty of foodstock

    feedings - holds feeding info
    notes - holds animal notes

    Now the issue I am having is that if the event is a feeding, or a note, I need to pull in the notes, or what exactly they ate. I thought I had a good design, but to get this info I am having to make some insane queries (for me at least) and I can’t figure them out.

    Is there a better way to structure the db? I could just add a notes and meal id to every entry, but this creates blank fields and pretty much boils down to what I use now.

    Suggestions / ideas?

    if you would like me to post the table structures with some play data I can do that.

    Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      No, you’re doing it right. Take a look at the queries in some of the action files in the MODx manager or the document parser itself if you want to see insane queries. Even fetching the TV values for a given document requires some heavy-duty SQL, especially considering that the value for a given document could be the TV’s default value if the one in the document itself is empty.

      $sql= "SELECT tv.*, IF(tvc.value!='',tvc.value,tv.default_text) as value ";
      $sql .= "FROM " . $this->getFullTableName("site_tmplvars") . " tv ";
      $sql .= "INNER JOIN " . $this->getFullTableName("site_tmplvar_templates")." tvtpl ON tvtpl.tmplvarid = tv.id ";
      $sql .= "LEFT JOIN " . $this->getFullTableName("site_tmplvar_contentvalues")." tvc ON tvc.tmplvarid=tv.id AND tvc.contentid = '" . $this->documentIdentifier . "' ";
      $sql .= "WHERE tvtpl.templateid = '" . $documentObject['template'] . "'";
        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
        • 10226
        • 412 Posts
        Yeah I am a lot closer to my goal... just one more thing left figure out.

        I had some useless junk in my schema, clearing that out simplified things.
          • 10226
          • 412 Posts
          SELECT 
            master_ev.ev_date,
            animals.aname,
            species.species,
            species.subspecies,
            IF(events_ref.ev_name = 'feeding', foodinv.food_type, IF(events_ref.ev_name = 'notes', notes.note, events_ref.ev_name)) AS event_fixed
          FROM
           master_ev
           INNER JOIN animals ON (master_ev.animal_id=animals.animal_id)
           INNER JOIN species ON (animals.species=species.species_idx)
           INNER JOIN events_ref ON (master_ev.events_idx=events_ref.event_id)
           LEFT OUTER JOIN notes ON (master_ev.ev_id=notes.ev_id)
           LEFT OUTER JOIN feedings ON (master_ev.ev_id=feedings.ev_id)
           LEFT OUTER JOIN foodinv ON (feedings.perm_id=foodinv.perm_id)


          Finally!
            • 10226
            • 412 Posts
            Turns out I was expecting the ability of nested if statements... So I had to learn some php laugh

            here is my end all solution for this nasty query smiley

            <?php
            
            include "dbcore.php";
            
            $query = "SELECT 
              master_ev.ev_date,
              animals.aname,
              species.species,
              species.subspecies,
              events_ref.ev_name,
              master_ev.events_idx,
              notes.note,
              weight.weight,
              foodinv.food_type
            FROM
             master_ev
             INNER JOIN animals ON (master_ev.animal_id=animals.animal_id)
             INNER JOIN species ON (animals.species=species.species_idx)
             INNER JOIN events_ref ON (master_ev.events_idx=events_ref.event_id)
             LEFT OUTER JOIN notes ON (master_ev.ev_id=notes.ev_id)
             LEFT OUTER JOIN weight ON (master_ev.ev_id=weight.ev_id)
             LEFT OUTER JOIN length ON (master_ev.ev_id=length.ev_id)
             LEFT OUTER JOIN feedings ON (master_ev.ev_id=feedings.ev_id)
             LEFT OUTER JOIN foodinv ON (feedings.perm_id=foodinv.perm_id)";
            	 
            $result = mysql_query($query) or die(mysql_error());
            
            echo "<table border='1'>";
            echo "<tr> <th>date</th> <th>animal</th> <th>species</th> <th>event</th> </tr>";
            
            while($row = mysql_fetch_array( $result )) {
            	echo "<tr><td>"; 
            	echo $row['ev_date'];
            	echo "</td><td>"; 
            	echo $row['aname'];
            	echo "</td><td>";
            	echo $row['species'];
            	echo "</td><td>";
            		switch ( $row['events_idx'] ) {
            			case 8:
            				echo $row['note'];
            				break;
            			case 5:
            				echo "weight ",$row['weight'],"oz";
            				break;
            			case 6:
            				echo "length ",$row['length'],"in";
            				break;
            			case 1:
            				echo $row['food_type'];		
            				break;
            			default:
            				echo $row['ev_name'];		
            		}	
            	echo "</td></tr>"; 
            } 
            
            echo "</table>";
            ?>


            Still many many things to learn!
              • 10450
              • 30 Posts
              Hey thr
              i used following syntax to select external database
              $ds = $modx->db->select(’*’,’database_name.table_name’);

              but i cant select multiple table at a time. can anybody tell me what i have to do?

              Thanks
                • 10226
                • 412 Posts
                You will probably get more love starting a new thread...

                I think you can only have one open db connection per php thread.

                Possibly rephrase your question as well. It is not exactly clear why you are wanting to do smiley
                  • 10450
                  • 30 Posts
                  Quote from: fruitwerks at Feb 15, 2010, 07:30 AM

                  You will probably get more love starting a new thread...

                  I think you can only have one open db connection per php thread.

                  Possibly rephrase your question as well. It is not exactly clear why you are wanting to do smiley

                  Thanks....

                  let me clear my question with example...

                  suppose i have external database XYZ with 5 tables...
                  i wanna join two tables of XYZ database let say Table "A" and Table "B"

                  But with the syntax i.e.
                  $ds = $modx->db->select(’*’,’database_name.table_name’);

                  i can select only one table....

                  Let me know if u r not able to understand my question

                  thanks again
                    • 10450
                    • 30 Posts
                    Quote from: ganesh_1707 at Feb 15, 2010, 07:49 AM

                    Quote from: fruitwerks at Feb 15, 2010, 07:30 AM

                    You will probably get more love starting a new thread...

                    I think you can only have one open db connection per php thread.

                    Possibly rephrase your question as well. It is not exactly clear why you are wanting to do smiley

                    Thanks....

                    let me clear my question with example...

                    suppose i have external database XYZ with 5 tables...
                    i wanna join two tables of XYZ database let say Table "A" and Table "B"

                    But with the syntax i.e.
                    $ds = $modx->db->select(’*’,’database_name.table_name’);

                    i can select only one table....

                    Let me know if u r not able to understand my question

                    thanks again

                    Hey pls help me someone