This code only shows categories with articles in them.
I want to show all categories.
Pls help.
$query = "SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C INNER JOIN jt_articles A ON C.id = A.catid GROUP BY C.id";
change to left join
SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C
LEFT JOIN jt_articles A ON C.id = A.catid GROUP BY C.id
Change INNER JOIN for LEFT JOIN in your query.
INNER JOIN looks explicitely for the join in the data
replace the inner join by a left outer join
Change INNER JOIN to LEFT JOIN.
Did u try LEFT JOIN ? bc. (i think) in second table u have NULL articles for some categories.
Related
the question is that i want to fetch all the field from different table with inner join and where condition.
I tried this query
$nowfire="select *
from reg_user r
inner join state s
on r.state=s.state_id
inner join country c
on r.country= c.country_id
where email_id='xyz#gmail.com'";
i know the another method is like this
$nowfire="select r.profile_id, r.email_id,s.state_name, c.city_name
from reg_user r
inner join state s
on r.state=s.state_id
inner join country c
on r.country= c.country_id
where email_id='xyz#gmail.com'";
this is working ...no problem.
but
can i used like in above frist *select * * ....format...
is this possible in mysqli....please help
Thanks in advance for any help.....
You can try:
$nowfire="select r.*,s.state_name, c.city_name
from reg_user r
inner join state s
on r.state=s.state_id
inner join country c
on r.country= c.country_id
where email_id='xyz#gmail.com'";
$nowfire="select r.*,s.*, c.*
from reg_user r
inner join state s
on r.state=s.state_id
inner join country c
on r.country= c.country_id
where email_id='xyz#gmail.com'";
I have this database design:
**users_tbl**
id
username
name
**posts_tbl**
id
url
users_id *FK REFERENCE to users table*
**posts_contents_tbl**
id
posts_id *FK REFERENCE to posts table
title
description
date
views
click
isDeleted
I'm using this query
SELECT a.name,a.username,c.*
FROM users_tbl a
LEFT JOIN posts_tbl b ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC
Why I try to run this query it gives me NULL results, sample output is like this
But when I try to remove the ORDER BY c.id ASC it gives me this output:
That's not my expected result.
My expected result would be it will display the posts_contents_tbl in Ascending order at the same time it won't show some null values. Some users in my database doesn't have posts data in the posts_tbl so they should not show too.
How would I do that one? Your help would be greatly appreciated and rewarded!
Thanks!
PS: I already have thousands record in my database.
In that case, you have to use INNER JOIN instead of LEFT JOIN because you only want users with posts to show. The reason why there are Null values is because the records are based on table users_tbl and you've mentioned that some of them have no post. Right?
Try this:
SELECT a.name,
a.username,
c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
INNER JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY c.`date` DESC
I think this is what you are looking for:
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY IFNULL(c.id, 0) ASC;
If you really needs that posts_tbl data should not display if not available.And all data of posts_contents_tbl then you need a RIGHT JOIN and INNER JOIN .
The Query like :-
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b ON a.id = b.users_id
RIGHT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC;
I just want to know how to get individual count of same value rows with join using mysql and help are definitely appreciated
Here is my mysql Query
SELECT c.`id`
FROM categories c inner join subcategories sc
on c.`id` = sc.`cat_id`
See the Response screeshot
try this query:
SELECT c.`id`, count(1)
FROM categories c inner join subcategories sc
on c.`id` = sc.`cat_id`
GROUP BY c.`id`
It's quite a basic task. Consider studying GROUP BY and aggregate functions.
SELECT id, COUNT(*)
FROM categories c
INNER JOIN subcategories sn ON c.id = sn.cat_id
GROUP BY c.id
I'm a little confused in here and need some help...
the situation is I've made three tables(fr_Leagues, fr_nations and fr_confeds), all i want to do is add a league which shows the name of the categories not the i.d with pagination. Here is the code:
NOW FIXED!
"SELECT
a.id as confed_id,
a.fr_short_name as confed_name,
b.id as nation_id,
b.fr_name as nation_name,
c.id as league_id,
c.fr_name as league_name"
." FROM fr_confeds as a
INNER JOIN fr_nations as b ON a.id = b.confed_id
INNER JOIN fr_leagues as c ON b.id = c.nation_id"
." LIMIT $paginate->start, $paginate->limit"
You are missing on how to link the different tables together. On each INNER JOIN, you need to have it:
INNER JOIN fr_nations ON a.<someColumn> = b.<anotherColumn> INNER JOIN fr_leagues ON a.<someColumn> = b.<anotherColumn>
USE THIS QUERY
SELECT * FROM fr_confeds as A
INNER JOIN fr_nations as B ON A.id = B.confed_id
INNER JOIN fr_leagues as C ON B.confed_id = C.league_id
LIMIT $paginate->start, $paginate->limit
I'm really sorry for the first post as i didn't explain everything.
Basically i have 3 tables, One for posts, One for Categories, & Another to link categories with posts.
I want in a single MySQL query to select posts that are under a specific category.
posts(id,title,body)
---------------------
125,Some title,Blah blah
categories(id,name)
---------------------
1,politic
2,entertainment
linker(categoryid,postid)
---------------------
2,125
I want in single query to fetch posts in the entertainment category by example, what to do?
Thanks
select
p.*
from
posts p
inner join linker l on l.postid = p.id
inner join categories c on c.categoryid = l.categoryid
where
c.name = 'entertainment'
The following SQL statement should provide you with a basis for what you are trying to do.
select p.*
from posts p
inner join linker l
on l.postid = p.id
inner join categories c
on l.categoryid = c.id
where c.name = 'entertainment'
If a post belongs to 2 categories, you can still use pinkfloydx33's query with DISTINCT in the select statement:
select
DISTINCT p.*
from
posts p
inner join linker l on l.postid = p.id
inner join categories c on c.categoryid = l.categoryid
where
c.name = 'entertainment'
The result set will show only one record.
It's the same exact thing, you just have to join 3 tables intead of 2 :
SELECT P.id post_id,
P.title,
P.body,
C.id category_id,
C.name
FROM posts P
INNER JOIN linker L
ON P.id = L.postid
INNER JOIN categories C
ON L.categoryid = C.id
WHERE C.name = 'Category'
Don't be afraid to do your own tests. If you understand how to join two tables, you should understand how to join three, four and more.
If you are specifying only one category in the WHERE clause, then the result will be a single row for each post ID.
Either way you can use DISTINCT or GROUP BY when the result could be more than one row per ID, but in that case i prefer the second one (GROUP BY).