0

I have got a SQL query to get data from my MySQL database:

SELECT wp_users.ID, wp_users.time, wp_users.display_name 
FROM wp_users 
INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id 
WHERE wp_usermeta.meta_key = 'wp_capabilities' 
AND wp_usermeta.meta_value LIKE '%user%' 
AND wp_users.time >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) 
ORDER BY wp_users.display_name";

It is getting a list of all users without the second AND statement:

AND wp_users.time >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) 

Is there anything I can do to have two ANDs to make it working?

Many thanks in advance

8
  • What's stored in the time column? (logically) Commented Sep 15, 2015 at 9:51
  • @DmytroShevchenko it has got UNIX_TIMESTAMP Commented Sep 15, 2015 at 9:51
  • What type does wp_users.time column have? Please, show some examples of values from this table. Commented Sep 15, 2015 at 9:51
  • What if you remove the first "AND" condition? Are you getting any data? Are you sure there actually is data in your table that fullfills all the conditions? It doesn't look like it. Commented Sep 15, 2015 at 9:53
  • @AlexeyShein wp_users.time has got a timestamp "1442310742" Commented Sep 15, 2015 at 9:53

1 Answer 1

2

I don't see an error with the query. I think the error is an artefact of your data set. From the comments on your question, I understand that the original query:

SELECT wp_users.ID, wp_users.time, wp_users.display_name 
FROM wp_users 
INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id 
WHERE wp_usermeta.meta_key = 'wp_capabilities' 
AND wp_usermeta.meta_value LIKE '%user%' 
AND wp_users.time >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) 
ORDER BY wp_users.display_name";

returns no results, but that removing the inner join and join conditions:

SELECT wp_users.ID, wp_users.time, wp_users.display_name 
FROM wp_users 
WHERE wp_users.time >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) 
ORDER BY wp_users.display_name";

produces what you were expecting. This implies that:

  1. there are users whose wp_users.time is within the target interval, but
  2. these users don't have corresponding entries in the wp_usermeta table and
  3. there are other users who do have corresponding entries, but they don't match the wp_users.time interval

Inner joins require matching key entries: http://www.w3schools.com/sql/sql_join_inner.asp

I think your choices are:

  1. alter the data set so that 100% of users participate in the wp_usermeta table
  2. convert the inner join to a left join or full outer join and relax the where constraints, e.g.: SELECT wp_users.ID, wp_users.time, wp_users.display_name FROM wp_users LEFT JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id ON wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '%user%' WHERE wp_users.time >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE)) ORDER BY wp_users.display_name;

That will technically make it work, but your query will be accepting users who only have NULLs for the corresponding (non-participating) wp_usermeta entries.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.