虚拟列mysql

时间:2016-05-25 16:38:56

标签: mysql

是否可以在此SELECT语句中添加虚拟列,并为每部手机添加产品的count()?

我想动态创建一个名为count的新列,它将是手机的产品数量。例如适用于iphone 5s的10款产品。

    $query"SELECT *, count(products.phone_id) as count..."

我的查询是:

    $query="SELECT * FROM phones INNER JOIN products on phones.id=products.phone_id";
    $query .= " WHERE phones.name LIKE '%". $searchInput . "%'";
    $query .= " AND phones.id IN (SELECT phone_id FROM products WHERE phone_id IS NOT NULL GROUP BY phone_id)";
    $query .= " GROUP BY phones.id";

1 个答案:

答案 0 :(得分:0)

试试这个

 $query="SELECT *,count(phones.id) as count FROM phones INNER JOIN products on phones.id=products.phone_id";
    $query .= " WHERE phones.name LIKE '%". $searchInput . "%'";
    $query .= " AND phones.id IN (SELECT phone_id FROM products WHERE phone_id IS NOT NULL GROUP BY phone_id)";
    $query .= " GROUP BY phones.id";