Hướng dẫn mysql multiple or
What you do here is called a But there are still X multiple rows in the result for every drink, if there are X photos with this particular drinks_id. Your statement doesn't restrict which photo(s) you want to have! If you only want one row per drink, you have to tell SQL what you want to do if there are multiple rows with a particular drinks_id. For this you need grouping and an aggregate function. You tell SQL which entries you want to group together (for example all equal drinks_ids) and in the SELECT, you have to tell which of the distinct entries for each grouped result row should be taken. For numbers, this can be average, minimum, maximum (to name some). In your case, I can't see the sense to query the photos for drinks if you only want one row. You probably thought you could have an array of photos in your result for each drink, but SQL can't do this. If you only want any photo and you don't care which you'll get, just group by the drinks_id (in order to get only one row per drink):
In MySQL, we also have GROUP_CONCAT, if you want the file names to be concatenated to one single string:
However, this can get dangerous if
you have 4.4.9 Using More Than one Table The
Given these considerations, the
As with the
Load the records like this:
Based on what you have learned from the queries that you have run on the Suppose that you want to find out the ages at which each pet had its litters. We saw earlier how to calculate ages from two dates. The litter
date of the mother is in the
There are several things to note about this query:
You need not have two different tables to perform a join. Sometimes it is useful to join a
table to itself, if you want to compare records in a table to other records in that same table. For example, to find breeding pairs among your pets, you can join the
In this query, we specify aliases for the table name to refer to the columns and keep straight which instance of the table each column reference is associated with. |