Answer the question
In order to leave comments, you need to log in
How to get unique field values based on another field?
Suppose there is a table with three fields id, product and color, product stores product types and color stores their colors, for example, product stores ten lighthouses and five sweaters, and color has 7 unique colors belonging to T-shirts (sweaters are not important), how to get all unique colors related to jerseys
Answer the question
In order to leave comments, you need to log in
<?php
$product = 'sweater';
$query = "select distinct color from products where product = ?;";
// get DB version using PDO
$stmt = $pdo->prepare($query);
$stmt->execute([$product]);
$colors = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($colors);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question