I have two tables in my SQL database:
mysql> select *from crop;
+------+-----------+----------+
| no | name | type |
+------+-----------+----------+
| 1 | pineapple | fruits |
| 2 | wheat | mainFood |
| 1 | apple | fruits |
| 2 | corn | main |
| 3 | rose | flower |
| 2 | wheat | main |
| 2 | maize | main |
| 1 | drydates | fruits |
+------+-----------+----------+
mysql> select *from enviornment;
+---------+------------+----------+------+
| climate | irrigation | soil | no |
+---------+------------+----------+------+
| humid | medium | alluvial | 2 |
| humid | medium | black | 1 |
| humid | medium | red | 1 |
| sunny | low | black | 1 |
| sunny | medium | alluvial | 1 |
| wet | high | red | 2 |
| humid | low | red | 3 |
+---------+------------+----------+------+
I want to get the name and type fields from the crop table, based on climate, soil, and irrigation.
I have written my query in the following way:
mysql> select T.name from((select name from crop)as T and (select no from envior
nment where climate like wet)as U)where T.no=U.no;
But when I try to execute it, I get the following error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘and (select no from enviornment where climate like wet)as U)where T.no=U.no’ at line 1
Can anyone tell me how to re-write my query to avoid this error?
You cannot use AND to construct query-results, it’s a logical operator. You can get all name, type, climate, soil and irrigation combinations with: