I can able to display list of product, but I want to show no of view count of the product from the view_product table
product_tbl table
----------------------------------
pro_id | product | description
----------------------------------
1 | prodcut1 | description
----------------------------------
2 | prodcut2 | description
----------------------------------
3 | prodcut3 | description
----------------------------------
view_product table
------------------------------------------
view_id | username | product | view_time
------------------------------------------
1 | usename1 | prodcut1 | 2019-10-31 11:45:00
------------------------------------------
2 | usename2 | prodcut1 |2019-10-31 11:46:00
------------------------------------------
3 | usename2 | prodcut1 |2019-10-31 11:45:00
------------------------------------------
4 | usename1 | prodcut1 |2019-10-31 11:46:00
------------------------------------------
$stmt = $conn->prepare("Select pro_id,product, description,qty from product_tbl");
$stmt->execute();
$row = $stmt-> fetchAll(PDO::FETCH_ASSOC);
$user_arr=array('data'=>$row);
echo json_encode($user_arr);
Based on product_id I want to view count of each product from the view_product table and Null valued row if the product no t viewed, how can I use the below query in PDO json encode.
select count(id) as count1 from view_product where user_id=".$row['pro_id'].";
Expected Output as below
------------------------------------------------
pro_id | product | description | view count
------------------------------------------------
1 | prodcut1 | description | 2
------------------------------------------------
2 | prodcut2 | description | 2
------------------------------------------------
3 | prodcut3 | description | NULL
------------------------------------------------
Avoid using n+1 queries, use a join statement instead in conjuction with count() function and in the end group them by a column.
SELECT
p.prod_id,
p.product,
p.description,
COUNT(v.view_id) AS view_count
FROM product_tbl AS p
LEFT JOIN view_product AS v ON p.product = v.product
GROUP BY v.product
Related
I am making a search paging, i have tried to select count from three table using union but am getting error like this
Fatal error: Cannot pass parameter 2 by reference in /home/plooks/public_html/user/test.php on line 106here is my table structure
table 1 name >> blogpost
| bid | title | body | author |
|----- |------- |------------- |-------- |
| 1 | new | hello new | you |
| 2 | cast | broadcast | me |
| 3 | hack | who hack us | you |
table2 name >> forumnew
| fid | ftitle | fbody | user |
|----- |------- |------------- |-------- |
| 1 | new forum | hello new | you |
| 2 | cast me | broadcast | me |
| 3 | hack you | who hack him | us |
table3 name >> download
| did | file | disc | type |
|----- |------- |------------- |-------- |
| 1 | whoweare | hello new | php |
| 2 | cast | broadcast | html |
| 3 | hack | who hack us | c++ |
SQL QUERY
SELECT COUNT(id) FROM (
SELECT 'post' AS type, BID AS id FROM blogpost
UNION
SELECT 'jail' AS type, jid AS id FROM forumnew
UNION
SELECT 'article' AS type, TID AS id FROM download
)csl WHERE title LIKE :search OR title LIKE :search")
There is a mistake in the query, you don't have the title in the temporary table csl, so it cannot be used in the WHERE clause. Also the second OR title LIKE :search is superfluous. If you don't want to do a distinct count of the id, then you don't need the other columnns, only the title column to filter by, the result will be the same.
SELECT COUNT(*) FROM (
SELECT title FROM blogpost
UNION
SELECT ftitle AS title FROM forumnew
UNION
SELECT disc AS title FROM download
) csl WHERE title LIKE :search
or
SELECT
(SELECT COUNT(*) FROM blogpost WHERE title LIKE :search) +
(SELECT COUNT(*) FROM forumnew WHERE ftitle LIKE :search) +
(SELECT COUNT(*) FROM download WHERE disc LIKE :search)
I have 2 tables, category and service
category_table
+----+--------------+
| id | category |
+----+--------------+
| 1 | category_1 |
| 2 | category_2 |
+----+--------------+
service_table
+----+--------------+-----------+
| id | service | cat_id |
+----+--------------+-----------+
| 1 | service_a | 1 |
| 2 | service_b | 1 |
| 3 | service_c | 1 |
| 4 | service_d | 2 |
+----+--------------+-----------+
I want to get the category count from the service table and display the result using the category name and limit it to 5 most used category
The desired result:
category name | category count
category_1 | 3
category_2 | 1
Seems like i have to join or leftjoin these table, but it is pretty confusing for me
You are right, you must use join's. You can rewrtie it with Eloquent, or use DB::raw() Try this:
SELECT c.category, COUNT(*) AS category_count FROM category_table c LEFT JOIN service_table s ON(c.id = s.cat_id) GROUP BY s.cat_id ORDER BY category_count DESC LIMIT 5;
P.S. I don't check it, but must work. Good tutorial about joins
DB::table('category_table')
->leftJoin('service_table', 'category_table.id', '=', 'service_table.cat_id')
->select(DB::raw('category_table.category,count(service_table.cat_id) as category_count'))
->groupBy('service_table.cat_id')
->order_by('category_count', 'desc')
->take(5)
->get();
I have the tables like this
tbl_post
+-----------+--------------+
| post_id | post_content |
+-----------+--------------+
| 1 | contentone |
+-----------+--------------+
tbl_category
+-------------+---------------+
| category_id | category_name |
+-------------+---------------+
| 1 | Politic |
| 2 | Social |
| 3 | Economy |
+-------------+---------------+
tbl_category_post
+------------------+-------------+---------+
| category_post_id | category_id | post_id |
+------------------+-------------+---------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
+------------------+-------------+---------+
then I want the output like this
+--------------+--------------------------+
| post_content | category |
+--------------+--------------------------+
| 1 | Politic, Social, Economy |
+--------------+--------------------------+
and then how to show the data like this using codeigniter, I really confused at all, anyone please help me!
Edit: With Codeigniter (not tested):
$this->db->select('post_id, GROUP_CONCAT(tc.category_name) AS category_name')
->from('tbl_category_post tcp')
join->('tbl_category tc', 'tc.category_id=tcp.category_id', 'left')
->group_by('tcp.post_id');
I suppose You need PHP loop method to loop this.
Use mysql GROUP_CONCAT function:
SELECT post_id, GROUP_CONCAT(tc.category_name) AS category_name
FROM tbl_category_post tcp
LEFT JOIN tbl_category tc ON tc.category_id=tcp.category_id
GROUP BY tcp.post_id
I would like to create and UPDATE MySQL query based on a SELECT statement that is already working.
So I have the following select statement that joins two tables - tbl_random and tbl_products by finding a random record from the second table:
$sql_select = "SELECT tbl_random.keyword, tbl_random.model_id,
tbl_products.make, tbl_products.model
FROM tbl_random LEFT OUTER JOIN
tbl_products
ON tbl_random.model_id = tbl_products.model
GROUP BY tbl_random.keyword
ORDER BY RAND()";
$rs_select = $db -> Execute($sql_select);
This is how tbl_random should look like after the update:
+---------+------------+---------+---------+--------------+
| keyword | model_id | make | model | more_data1 |
+---------+------------+---------+---------+--------------+
| apple1 | 15 | app5 | 15 | data1 |
| apple2 | 15 | app1 | 15 | data2 |
| pear | 205 | pear53 | 205 | data3 |
| cherry | 307 | cher74 | 307 | data4 |
| melon | 5023 | melo2 | 5023 | data5 |
+---------+------------+---------+---------+--------------+
What UPDATE query should I use in order to the able to update the make and model fields in tbl_random, with some respective random values from tbl_products?
I have a car booking system database that i am in the process of building. I have 3 tables:
car_table:
ID | car_make | car_name
1 | Ford | Focus
2 | BMW | Z3
3 | Audi | A5
booking_table:
booking_ID | booking_time | total_cost | etc..
125674 | 2013-02-02 | 91.55
887463 | 2013-01-19 | 52.00
209930 | 2013-01-11 | 23.99
reservation_table:
ID | booking_ID | car_ID
1 | 125674 | 2
2 | 887463 | 2
3 | 209930 | 1
So the reservation_table links the cars with the bookings. Now i am wanting to output the following result in a table for the user:
booking_ID | booking_time | total_cost | car_ID | car_make | car_model
How can i output the results for this, so far i have this almost working:
$query = "
SELECT
booking_ID,
total_cost,
booking_time,
customer_ID,
car_ID
FROM
bookings_table as bb,
reservation_table as br
WHERE
bb.booking_ID = br.booking_ID AND
payment_success=true
ORDER BY
booking_ID desc
LIMIT
10
";
try {
$stmt = DB::get()->prepare($query);
$stmt->execute();
$rows = $stmt->fetchAll();
}
catch(PDOException $ex) {
// Failed
}
foreach($rows as $row):
<td>'.$row['booking_ID'].'</td>
etc...
endforeach;
Which displays as below:
booking_ID | booking_time | total_cost | car_ID | car_make | car_model
125674 | 2013-02-02 | 91.55 | 2 | |
So i can get the car_ID from the reservation_table but i don't know how i can also pull out the car_make/model by taking the car_ID from the reservation_table.