开发者

Pulling data from MySQL only if a certain field is not empty

I'm trying to use a WHERE statement to pull info where the field t.twittername is not empty. The code below doesn't work. What kind of WHERE statement can I use?

EDIT: I tried WHERE t.twittername IS NOT NULL but that stil开发者_JAVA技巧l didn't work.

EDIT: I am now showing the full SQL statement.

Here is the SQL for the table tweets:

CREATE TABLE IF NOT EXISTS `tweets` (
  `tweetid` bigint(11) NOT NULL AUTO_INCREMENT,
  `city` varchar(100) CHARACTER SET latin1 NOT NULL,
  `submissionid` varchar(11) CHARACTER SET latin1 NOT NULL,
  `shorturl` varchar(1000) CHARACTER SET latin1 NOT NULL,
  `loginid` varchar(11) CHARACTER SET latin1 NOT NULL,
  `twittername` varchar(1000) CHARACTER SET latin1 NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`tweetid`)

Code:

 $sqlStr3 = "SELECT
                 l.username 
                 ,l.loginid  
                 ,t.tweetid 
                 ,t.shorturl 
                 ,t.twittername 
                 ,t.time
                 ,s.submissionid 
                 ,s.title
                 ,s.url 
                 ,s.displayurl 
                 ,s.datesubmitted
                 ,s.points
                 ,COUNT(t.tweetid) countTweets 
        FROM tweets AS t        
      WHERE TRIM (t.twittername) IS NOT NULL AND TRIM(t.twittername) != ''
  INNER JOIN login AS l ON t.loginid = l.loginid
  INNER JOIN submission AS s ON t.submissionid = s.submissionid
        GROUP BY t.tweetid  
    ORDER BY t.time DESC
       LIMIT $offset, $rowsperpage";


try

WHERE t.twittername IS NOT NULL 

EDIT - after comment:

try this "extreme" version for checking a string field for emptiness:

WHERE TRIM (t.twittername) IS NOT NULL AND TRIM(t.twittername) != ''

EDIT 2 - another point:

Your SQL statement is odd - you refer to s but I can't see an s and you use COUNT but I don't see a GROUP BY and I would expect some sort of JOIN too which I don't see... please show the real statement...

EDIT 3 - after the OP showed the statement and table definition:

I actually don't understand what this statement should generate BUT give the following a try:

SELECT
             l.username 
             ,l.loginid  
             ,t.tweetid 
             ,t.shorturl 
             ,t.twittername 
             ,t.time
             ,s.submissionid 
             ,s.title
             ,s.url 
             ,s.displayurl 
             ,s.datesubmitted
             ,s.points
             ,COUNT(t.tweetid) countTweets 
    FROM tweets AS t        
INNER JOIN login AS l ON t.loginid = l.loginid
INNER JOIN submission AS s ON t.submissionid = s.submissionid
  WHERE TRIM (t.twittername) IS NOT NULL AND TRIM(t.twittername) != ''
    GROUP BY 
             l.username 
             ,l.loginid  
             ,t.tweetid 
             ,t.shorturl 
             ,t.twittername 
             ,t.time
             ,s.submissionid 
             ,s.title
             ,s.url 
             ,s.displayurl 
             ,s.datesubmitted
             ,s.points
ORDER BY t.time DESC
   LIMIT $offset, $rowsperpage


You can test if a column is null by using the is operator :

-- ...
WHERE t.twittername IS NOT NULL;

Also, do not confuse the lack of data (NULL) and en empty string ('', which is a value). If you want to test if the string is empty, do something like this :

-- ...
WHERE t.twittername = '';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜