Crosstab query in mysql using phpmyadmin - mysql

I have 2 tables in mysql, tbl_post & tbl_comment,
I require a crosstab query , based upon tbl_post.post_id, the result should be like
all elments of tbl_post + count of records form tbl_comment, where
tbl_post.post_id == tbl_comment.post_id
e.g. Result should be like ::
post_id,title,content,tags,status,create_time,update_time,author_id,likes + count from tbl_comment
Please see the image.
I am new to sql just having academic knowledge , and couldn't figure it out. Any help is appreciated.

I think you just need to join tbl_post to a subquery which counts the number of comments for each post.
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
LEFT JOIN
(
SELECT post_id, COUNT(*) AS post_count
FROM tbl_comment
GROUP BY post_id
) t2
ON t1.post_id = t2.post_id
If you want to create a view using the above query then you need to get a bit creative. The following attempt will fail because the above query has a subquery in it:
CREATE VIEW PostCountView AS
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
...
Instead, you can create a view for the subquery, and then use that in a second view for the main query:
CREATE VIEW PostCountView AS
SELECT post_id, COUNT(*) AS post_count
FROM tbl_comment
GROUP BY post_id
CREATE VIEW PostCountMainView AS
SELECT t1.*,
COALESCE(t2.post_count, 0) AS post_count
FROM tbl_post t1
LEFT JOIN PostCountView t2
ON t1.post_id = t2.post_id

select t1.post_id,
t1.title,
t1.content,
t1.tags,
t1.status,
t1.create_time,
t1.updated_time,
t1.author_id,
t1.likes,
count(t2.post_id)
from tbl_post t1
LEFT JOIN tbl_comment t2
on t1.post_id = t2.post_id
group by t1.post_id;

Related

select join two table then order by latest upload time(uplaod_time column) in mysql select query

I got two table users(table 01)、record_dcm_upload(table02)
i try to query counts and latest upload file time by everylogin account(users.username)
like
SELECT record_dcm_upload.user_id, users.username, record_dcm_upload.upload_time, COUNT( * )
FROM record_dcm_upload
JOIN users ON ( users.id = record_dcm_upload.user_id )
GROUP BY record_dcm_upload.user_id
but my query sql got some problem (actually the result of upload_time not the latest)
how should i adjust my query code (hope user_id and upload_time all sort By DESC)
SELECT t2.id, t2.username, MAX(t1.upload_time), COUNT(*)
FROM record_dcm_upload t1
JOIN users t2 ON ( t2.id = t1.user_id )
GROUP BY t2.id, t2.username

Join with column outside subquery

SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount
FROM database2.table
WHERE ids IN (t1.values)
) as t2
WHERE t1.id = 20;
I get an error, that t1.values inside the subquery is unknown column.
You need to rewrite your query and take inne where to join condition:
SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount
FROM database2.table
) as t2 ON t2.ids = t1.values
WHERE t1.id = 20;
Also, you don't use amount column, so what is the point of join?
Another issue, you don't have any join condition defined.
I think you need to read about joins in SQL first :)
It seems you are trying to join database2.table to your t1 based on t1.values list.
I added group by IDs in t2 since your using aggregation function. Then, not sure what's the purpose of your sum(amount)
SELECT t1.name as r_name, t1.values as r_values
FROM table as t1
JOIN (
SELECT SUM(amount) as amount, ids
FROM database2.table
GROUP BY ids
) as t2 on t2.ids IN (t1.values)
WHERE t1.id = 20;

SQL query to find comparision between different products

Hi i am a newbie to SQL and have one doubt on comparing different products across multiple tables.
I have 3 tables
T1:
Product_type
order_id
T2 and T3 also has the same fields.
All the tables have different product types. They may or may not have same order ids. Its like you can order product p1 from T1 and product p2 from T2 together on the same order id o1 or they can be separate orders.
I want to find the number of orders where product type(p1) from T1 and product type(p2) from T2 are ordered in the same order(having the same order id).
I am trying to run the query like this :
select COUNT(DISTINCT order_id) as CountOf from
(
select product_type from t1
UNION ALL select product_type from t2
)
AS m
where t1.product_type = p1 and t2.product_type = p2;
What i figured out is that i cannot access t1 and t2 in the outer query since they are used in the inner query. So is there a way i can make comparision between products?
Any help would be greatly appreciated.
Thank you
Try this:
select
count(distinct t1.order_id) as OrderCount
from
t1
inner join
t2 on t1.order_id = t2.order_id
where
t1.product_type = 'p1' and
t2.product_type = 'p2'
I can't understand what you want.
But inner query scope to brackets only. So you can not access outside to brackets. try to this
select COUNT(DISTINCT order_id) as CountOf from
(
select DISTINCT order_id from t1
where t1.product_type = p1
UNION ALL
select DISTINCT order_id from t2
where t2.product_type = p2
) m

Why do I receive an error at this join tables?

I wrote this:
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN (SELECT *
FROM T
WHERE T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC
LIMIT 10)
and I receive this
"Unknown column 'T1.CATEGORY' in 'where clause'".
Why?
Update:
My purpose of this is to get 10 posts of any category.
Because T1 is not visible from within the subquery.
Your JOIN also serves no purpose and/or you probably forgot the JOIN condition.
In JOIN condition should use ON keyword
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN SELECT * FROM T ON T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC LIMIT 10;
If you need to get 10 posts of each category you can use a query like this:
SELECT CATEGORY, Post
FROM (
SELECT a.CATEGORY, a.Post, count(*) as rn
FROM #T a
JOIN #T b ON a.CATEGORY = b.CATEGORY AND a.Post >= b.Post
GROUP BY a.CATEGORY, a.Post) dt
WHERE rn < 11;

mysql inner join on multiple select statements

Trying to do an inner join on two composite tables, having trouble with the syntax. Here's what I have:
SELECT
count(*)
FROM
(
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID t1
INNER JOIN
(
SELECT DISTINCT UserID FROM Bids, Users WHERE Bids.UserID = Users.userID
)
t2 ON t1.userID = t2.userID
)
I'm guessing it has something to do with the parantheses/lack of as/or whatever. I guess what I'm really asking here is how to give my subqueries aliases, but not using as in the FROM part. Is it valid just to have t1 after User.userID and identify that whole table as t1?
I think this is what you want?
SELECT count(*)
FROM Users
INNER JOIN Items ON Users.userID = Items.seller
INNER JOIN Bids ON Users.UserID = Bids.UserID
You want to name the output table which you get from query
SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID
as t1
simple way is use
`select * from (SELECT DISTINCT seller FROM Items, Users WHERE Items.seller = Users.userID)t1`