There is my query to get contacts from the database:
$contacts = $dbh->prepare("
SELECT r.title region_title
, c.title
, c.region_id
, c.id
, c.catalog_id
, c.address
, c.phone
, c.email
, c.website
, c.category_title
, c.subcategory_title
, c.subcategory_id
, c.manufacturer
FROM contacts c
LEFT
JOIN regions r
ON c.region_id = r.id
WHERE manufacturer = 1
AND region_id IN (".implode(',', $regions).")
AND subcategory_id IN (".implode(',', $categories).")
");
And getting a list of contacts grouping buy region_title
$contacts->fetchAll(PDO::FETCH_GROUP)
Result is:
array() {
["First region title"]=>
array() {
contact1
contact2
...
},
["Second region title"]=>
array() {
contact3
contact4
...
}
}
How can I get the number of unique c.catalog_id fields in the same query?
You can get unique catalog_id fields by groupping by catalog_id and set having condition as below:
$contacts = $dbh->prepare("
SELECT r.title region_title
, c.title
, c.region_id
, c.id
, c.catalog_id
, c.address
, c.phone
, c.email
, c.website
, c.category_title
, c.subcategory_title
, c.subcategory_id
, c.manufacturer
FROM contacts c
LEFT
JOIN regions r
ON c.region_id = r.id
WHERE manufacturer = 1
AND region_id IN (".implode(',', $regions).")
AND subcategory_id IN (".implode(',', $categories).")
GROUP BY catalog_id HAVING COUNT(catalog_id)=1
");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.