11

Im trying to run a sub-query that based on one of the main query values but i always get 0 as VALUE. This is my query :

SELECT ID,(
SELECT COUNT( * ) 
    FROM  `post_meta` 
    WHERE  `post_id` 
    IN (
        SELECT  `ID` 
        FROM  `wp_posts` 
        WHERE  `post_title` = posts.ID
    )
) AS counter
FROM wp_posts;

if i run only the sub-query with id number instead of posts.ID it returns a good value.

3
  • I think you still have something mixed up. I am assuming you meant wp_posts.ID due to your comment on jjs9534 answer. So, how can you have wp_posts.ID = post_meta.post_id and also wp_posts.ID = post_meta.post_title? That just doesn't seem right. Commented Dec 5, 2013 at 21:12
  • Thanks for your comment, it does not seem right but it is, the last programmer who built this database didnt do a good job with it so the post_title sometimes used for titles and sometimes to store id, if i change the WHERE post_title = posts.ID to post_title = 5161 it works fine, so this part is ok, Commented Dec 5, 2013 at 21:21
  • I still don't understand the logic. If your post_meta.post_id is always populated then you couldn't you just leave off the wp_posts.ID = post_meta.post_title completely. Part of the problem might be your comparing two different data types. Commented Dec 5, 2013 at 21:26

3 Answers 3

5

I do believe a simple join in the sub query will get you the correct COUNT:

SELECT posts.ID,
(
  SELECT COUNT(*) 
  FROM post_meta
  INNER JOIN wp_posts ON wp_posts.ID = post_meta.post_ID
  WHERE wp_posts.post_title = posts.ID  
) AS counter
FROM posts;
Sign up to request clarification or add additional context in comments.

Comments

2

The problem was fixed by giving the table a custom name so i can use it when im going in two layers this is how the code look after the change :

SELECT ID,(
SELECT COUNT( * )
    FROM  `post_meta` 
    WHERE  `post_id` 
    IN (
        SELECT  `ID` 
        FROM  `wp_posts` 
        WHERE  `post_title` = my_posts.ID
    )
) AS counter
FROM wp_posts as my_posts;

Comments

1

Linger you beat me to it but I was going to suggest leaving out the subquery altogether if I can guess what the structure looks like.

SELECT ID, COUNT(*) AS count FROM wp_posts
INNER JOIN post_meta ON wp_posts.ID = post_meta.post_id
WHERE wp_posts.ID = post_title
GROUP BY ID

OR

SELECT COUNT(*) AS count FROM wp_posts 
INNER JOIN post_meta ON wp_posts.ID = post_meta.post_id
WHERE wp_posts.ID = post_title

5 Comments

Hi, thanks for you answer, I had a mistake when writing this query there is only wp_posts table (no posts table) I have tried to write the query with inner join as you suggested but had no success, how can i write this query in my case? (i have edited the question)
I edited my answer based on your edit. It looks like you are just asking for the number of meta posts for a particular post id.
that's correct but both of the queries do not work, the first one dont fetch any row and the second one get always 0 as a value like it was before
@user2326568, see, my comment above on your question, you have something mixed up still.
@Linger that is what I was thinking too, it looks like he is comparing post title to post id, but hey...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.