<div dir="ltr"><div>Heh. Hadda look that one up. Could work great, depending on resources and purpose.<br><br></div>-- CHS<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 13, 2015 at 2:51 PM, DJ-Pfulio <span dir="ltr"><<a href="mailto:DJPfulio@jdpfu.com" target="_blank">DJPfulio@jdpfu.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Or just use an ORM.<br>
<span class=""><br>
On 03/13/2015 02:36 PM, Charles Shapiro wrote:<br>
> Hmm. I'm not that familiar with MySQL, but in PostgreSQL the way to determine<br>
> what you want is<br>
><br>
> select<br>
</span>> <a href="http://animal.id" target="_blank">animal.id</a> <<a href="http://animal.id" target="_blank">http://animal.id</a>><br>
<span class="">> from<br>
> animal<br>
> left join<br>
> food<br>
> on<br>
</span>> <a href="http://animal.id" target="_blank">animal.id</a> <<a href="http://animal.id" target="_blank">http://animal.id</a>> = food.animalid<br>
> where<br>
> <a href="http://food.id" target="_blank">food.id</a> <<a href="http://food.id" target="_blank">http://food.id</a>> is null<br>
<span class="">> ;<br>
><br>
><br>
> Writing SQL queries takes some practice. One handy tip I've found is to write<br>
> the "from..." part first, then go back and fill in what columns you wish to select.<br>
><br>
> Doing anything based on the numeric value of the "id" field is a Bad Idea. In<br>
> most relational databases, that field just increments when you add a record to<br>
> your table. It never decrements when you delete records. That means that the<br>
> ID value itself is useless in determining what is or is not in a table. It's<br>
> only useful when it's in another table -- hence the name, "Relational".<br>
><br>
><br>
> Your current table setup also breaks down if you add -- say -- a goat which<br>
> wants to eat the same things as the giraffe. You're more likely to have several<br>
> animals linked to the same foods than several foods linked to the same animal.<br>
> Don't you really want a "FoodFk" in the "animal" table? Then you could have<br>
> lots of animals which eat the same thing. Of course, I reckon in reality you'd<br>
> need a jump table to link foods and animals:<br>
><br>
> food_animal { integer animalid references animal(id), integer foodid references<br>
> food(id) }<br>
><br>
> Then you could have lots of different foods and lots of different animals all<br>
> linked together.<br>
><br>
><br>
> -- CHS<br>
><br>
><br>
> On Fri, Mar 13, 2015 at 1:52 PM, David S Jackson <<a href="mailto:deepbsd.ale@gmail.com">deepbsd.ale@gmail.com</a><br>
</span><span class="">> <mailto:<a href="mailto:deepbsd.ale@gmail.com">deepbsd.ale@gmail.com</a>>> wrote:<br>
><br>
> Hi,<br>
><br>
> I'm a MySQL newbie. I'm just starting to use Python to talk to MySQL<br>
> databases, but first I need to understand the MySQL query language!<br>
><br>
><br>
> So, I have two tables: (zoo) animal and food.<br>
><br>
> animal has the columns ID, NAME, FAMILY, WEIGHT<br>
><br>
> food has ID, ANIMALID, FEED<br>
><br>
><br>
> So if I go: select id, name, family, feed from animal JOIN food ON <a href="http://animal.id" target="_blank">animal.id</a><br>
</span>> <<a href="http://animal.id" target="_blank">http://animal.id</a>>=food.animalid;<br>
<span class="">><br>
><br>
> I get something like:<br>
><br>
> ID NAME FAMILY FEED<br>
> 1 Seymore Snake mice, leaves<br>
> 2 Gerard Giraffe leaves, grass<br>
> ...<br>
> etc<br>
><br>
> So, if I have a situation where I want to see whether an animal has been<br>
> added the animal table but may have not been added to the food table, how<br>
> would I compose that query?<br>
><br>
> That is, the animal could have been added to the animal table and could get<br>
> an id, say 10, but the food table could have 20 foods already entered and<br>
> the animal-id would be used several times.<br>
><br>
> I was thinking, is there a way I can ask, "does the highest <a href="http://animal.id" target="_blank">animal.id</a><br>
</span>> <<a href="http://animal.id" target="_blank">http://animal.id</a>> number equal the highest food.animalid number"? If not,<br>
<div class="HOEnZb"><div class="h5">> what animal is not getting fed?<br>
><br>
> Dave<br>
><br>
_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org">Ale@ale.org</a><br>
<a href="http://mail.ale.org/mailman/listinfo/ale" target="_blank">http://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
</div></div></blockquote></div><br></div>