E
E
Egorithm2016-05-19 16:44:51
MySQL
Egorithm, 2016-05-19 16:44:51

MySQL. How to write a query with a subquery?

There is a product table , in this table there are 3 fields: maker (manufacturer), model (product model) and type (product type).
I'm required to list manufacturers that make pc but don't make laptop .
This is the query I got:

SELECT DISTINCT maker FROM product
WHERE type LIKE "PC%" AND type <> (
SELECT DISTINCT maker FROM product
WHERE type LIKE "Laptop%");

The error is: ERROR 1242 (
21000): Subquery returns more than 1 row

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2016-05-19
@EgoRusMarch

SELECT `p1`.`maker`
  FROM `product` AS `p1`
  LEFT `product` AS `p2` ON `p2`.`maker` = `p1`.`maker`
    AND `p1`.`type` LIKE 'PC%' AND `p2`.`type` LIKE 'Laptop%'
  WHERE `p2`.`maker` IS NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question