Skip to content
Advertisement

How to find a non-existing value from another table in mysql using ONE single QUERY

I want to find number of types of produce of each farmer, names of farmer and the total number of produces but I am getting stuck in the part trying to display farmers who do not produce anything in the supplies and then in another query I want to extend it to display farmers with highest produces but without using the limit function.

My current attempt which is not working

The output should be

Advertisement

Answer

This query gives “number of types of produce of each farmer, names of farmer and the total number of produces”.

This query gives “farmers who do not produce anything in the supplies”.

The restriction – the lost relations (the rows in Supplies where FarmerId has definite value but ProduceID is NULL) not exists. For to guarantee this define both mentioned columns as NOT NULL in table structure.


The output should be ..

Left joining does not allow to reject the farmer rows which has no according rows in another tables. COALESCE function replaces NULLs obtained for such rows with zero value.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement