there are two tables , blog and comments table. in blog table all blogs are stored an
Blog Table
----------------------------------------
id | blog_name | date
----------------------------------------
1 | abc | 31/3/17
----------------------------------------
2 | xyz | 31/3/17
----------------------------------------
3 | rbc | 31/3/17
----------------------------------------
4 | ert | 31/3/17
----------------------------------------
Comment Table
----------------------------------------
id | comment | blog_id | approved
----------------------------------------
1 | abc | 1 | 0
----------------------------------------
2 | xyz | 1 | 1
----------------------------------------
3 | rbc | 2 | 0
----------------------------------------
4 | ert | 4 | 1
----------------------------------------
now i want to show results the way .. that all blogs will show but with the count of comments with each blog.. but the comment count should be only for approved comments.. by default the approved comment is 0 means not approved but 1 means approved..the problem is if i join the two tables in where condition i write approved=1 then it shows only 2 blogs but i need to show all blogs with comments.. if any blog comment is not present in the comment table then it will show 0 count to me... please help me with this
im using this join.. this join not showing all blogs..
SELECT b.id,b.blog_name,COUNT(c.comment) as comment_count FROM blog b LEFT JOIN comment c ON b.id=c.blog_id WHERE c.approved=1 GROUP BY b.id
i want all blogs with comments.. it shows only approved comments related blogs..
SELECT
blog.*,
COALESCE(COUNT(comments.id),0) AS comment_count
FROM blog
LEFT JOIN comments
ON comments.blog_id = blog.id AND comments.approved = 1
GROUP BY blog.id
Related
I need to query data from 4 tables. I'm not sure this is the best db design to approach this. This is simplified version. The actual table have more column.I have table for users who upload their song:
Id | Username |
---------------
1 | John |
2 | Michael |
3 | Frank |
Then I have song table like this
Id | Title | UserId
----------------------
1 | Title 1 | 1
2 | Title 2 | 1
3 | Title 3 | 2
4 | Title 4 | 2
Then a playlist table like this
Id | Title | UserId
------------------------
1 | My Playlist | 3
Then a playlist entries where a user save song id belong to a playlist
Id | SongId | PlaylistId
--------------------------
1 | 2 | 1
2 | 4 | 1
3 | 3 | 1
What I want to achieve after query is something like this:
Playlist | By | Song List
------------------------------------------------------------
My Playlist | Frank | Title 2 - John, Title 3 - Michael,
| | Title 4 - Michael
-------------------------------------------------------------
Any idea how to query this in MySQL. Or any better table design to achieve same result?
Try this:
SELECT
p.Title as Playlist,
u1.Username as By,
group_concat(s.Title, ' - ', u2.Username order by s.Id) as `Song List`
FROM playlist p
JOIN playlist_entries pe
ON p.Id= pe.PlaylistId
JOIN song s
ON pe.SongId = s.Id
JOIN user u1
ON p.UserId = u1.Id
JOIN user u2
ON s.UserId = u2.Id
Demo Here
I am developing a photo sharing app platform. The app allows you to post a photo and others can like or rate the photo. Users can follow each other and see photos their 'followings' are sharing, just like instagram.
#user_tbl
id | name | number
-------------------
1 | Dan | 0209
2 | Sam | 2854
3 | Dave | 8123
4 | Alex | 5600
#photo_tbl
id | userid | path
-------------------
1 | 3 | dave-dog.jpg
2 | 1 | dans-cat.png
3 | 4 | alex-bird.jpg
4 | 2 | sam-fish.jpg
#friendship_tbl
id | actor | target
--------------------
1 | 2 | 1 // Sam is following Sam
2 | 2 | 4 // Sam is following Alex
3 | 1 | 3 // Dan is following Dave
4 | 4 | 2 // Alex is following Sam
#activities_stream_tbl
id | photoid | userid | context | date
----------------------------------------------------------
1 | 3 | 4 | add-new-photo | 10/10/2015
2 | 1 | 3 | add-new-photo | 12/10/2015
3 | 3 | 2 | Sam-share-Alex-photo | 15/10/2015
4 | 4 | 2 | add-new-photo | 20/10/2015
6 | 1 | 1 | Dan-like-Dave-photo | 21/10/2015
The #user_table holds the basic info of a user, while #photo_tbl hold the name and path of the photo shared by the user. In the #friendship_tbl is the relationship link between users. "actor" column is the id of the user doing the following while "target" column is the id of the user being followed.
I am currently having problem writing a query string to pull photos of USERX and photos of other users USERX is following and GROUP them by "photoid" in the activities_stream_tbl and ORDER BY "date" activities_stream_tbl.
I will be glad if anyone can help me, show me a better way of structuring db thank you.
to pull photos of USERX, you can construct your sql like
select PATH
from user_tbl as a inner join photo_tbl as b
on a.id = b.user_id
and a.name = 'userx'
and to pull photos of other users USERX is following, you may write
select path
from photo_tbl as a
where a.userid in (select target from friendship_tbl as x inner join user_tbl as y on x.actor = y.id and y.name = 'user')
you can union the above two results if you want.
ex:
select PATH
from user_tbl as a inner join photo_tbl as b
on a.id = b.user_id
and a.name = 'userx'
UNION
select path
from photo_tbl as a
where a.userid in (select target
from friendship_tbl as x
inner join user_tbl as y
on x.actor = y.id and y.name = 'user')
I would like to know how to select from one table and count from another (loop) using a single query
Table ctagories
------------------------------
cat_id | cat_name | parent_id
------------------------------
1 | General | 0
------------------------------
2 | News | 0
------------------------------
3 | Sports | 1
------------------------------
4 | Test | 0
------------------------------
Table posts
--------------------------------------
post_id| title | c_id | active
--------------------------------------
1 | test | 1 | 1
--------------------------------------
1 | test 1 | 2 | 0
--------------------------------------
1 | test 2 | 1 | 1
--------------------------------------
1 | Test 3 | 3 | 1
--------------------------------------
I want display categories where parent_id=0 (main categories) with the post count (posts where active = 1) in front of it
Ex: General (2 posts)
Can anyone give me a example how to do it with a one query
SELECT
`cat_name`,
(SELECT COUNT(*) FROM `posts` p WHERE p.active =1 AND p.c_id = c.cat_id) as post_count
FROM `category` c
WHERE c.parent_id = 0
ORDER BY `cat_name` ASC
try this query ,you can use subquery with select statement in query.
select cat_name,
(select count(*)
from post
where active=1
and c_id=cat_id
) as countpost
from ctagories
where parent_id=0;
I cant figure out how to get comments underneath mysql db entries..
For example There is one article witch id is 1 and then there are some (for example 3) comments in that article (ID - 1) [ARTICLE_ID][CATEGORY]
And the comments are like: [ID][ARTICLE_ID]
..
so it is like
| ID | SUB_ID | TITLE....
| 1 | 123 | THIS IS A ARTICLE
| 2 | 1 | COMMENT TO ARTICLE 1
| 3 | 1 | COMMENT 2 to article 1
| 4 | 1 | Comment 3 to Article 1
AND CEN_TYPE for all of thesese IDs, 1,2,3,4 is forumentry
I've tryed like this:
SELECT everthing from CNT_Entries_A WHERE CEN_TYPE is like forumentry - so everthing is fine here, but it doesnt put comments underneath (logically)
Putting all ID's AND SUB_ID's in to a array's
making foreach cycle, inside that checking if current ID is equal to arrays value if it is, check if that ID is in array - if is not then printing it out and putting it to an array..
SAME THING to SUB ID
I highly recommend to split the data into two data tables:
Articles
article_id | text
Comments
comment_id | article_id | comment
Then You won't have problem to do a simple SELECT + LEFT JOIN query...
This will give result as article and their comments so that u can then loop over:
find the implementation here
SELECT c1 . * , c2 . *
FROM comments AS c1
JOIN comments AS c2 ON ( c1.id = c2.sub_id
AND c1.id != c2.id )
WHERE c1.cen_type = 'forumentry'
+----+--------+-------------------+------------+----+--------+----------------------+------------+
| id | sub_id | title | cen_type | id | sub_id | title | cen_type |
+----+--------+-------------------+------------+----+--------+----------------------+------------+
| 1 | 1 | THIS IS A ARTICLE | forumentry | 2 | 1 | COMMENT TO ARTICLE 1 | forumentry |
| 1 | 1 | THIS IS A ARTICLE | forumentry | 3 | 1 | COMMENT 2 to article | forumentry |
| 1 | 1 | THIS IS A ARTICLE | forumentry | 4 | 1 | Comment 3 to Article | forumentry |
+----+--------+-------------------+------------+----+--------+----------------------+------------+
I have two tables one that contains a huge list of items and another that trading for those items.
Here are examples tables:
The main table
| ID | TITLE | STATUS | TRADE |
-------------------------------
| 1 | test1 | 1 | 1 |
| 2 | test2 | 1 | 1 |
| 3 | test3 | 1 | 0 |
| 4 | test4 | 0 | 1 |
The trade table
| ID | TRADER | ITEM | URL |
------------------------------------------------------
| 1 | 2 | 1 | HTTP://www.test.com/itemOne |
| 2 | 5 | 3 | HTTP://www.test.com/itemThree |
| 3 | 5 | 4 | HTTP://www.test.com/itemFour |
Say I want to have a list of all the items that are not being traded by trader 5 and have a status of 1. So when trader 5 comes to the site they will be able to select the remaining items to trade.
Here is what I have tried:
$sql = "SELECT m.id, m.title
FROM main AS m, trade AS t
WHERE m.trade >= 1 && m.status = 1 &&
t.trader <>". mysql_real_escape_string($traderID);
This code just doesn't work. Any ideas on this?
It is not clear to me what column in Trades is an FK to Main. Below, I have assumed it is the Item column:
select m.id, m.title
from Main m
where not exists (
select *
from trade
where m.id = item
and trader = 5
)
and m.status = 1
Try this:
SELECT id, title FROM main
WHERE status = 1 AND id NOT IN
(SELECT item FROM trade WHERE trader = 5);
This will grab a list of every title in main with a status of 1, but limit the items based on a subquery which gets a list of ids already traded by trader 5 (i.e. items "not in" the list of items returned as having been traded by trader 5).
I'll leave it to you to update the query to be parameterized as needed.
Note that I'm assuming that item in trade is a foreign key to the id field in main, since you didn't specify it.