0

I have three tables,

FEED - USERS - FOLLOWING

I am trying to display a feed where either USERNAME = $_SESSION[username] OR if the currently logged in user is following someone in the FOLLOWING table

Here is my current query that selects all from feed based just on USERNAME and NOT including FOLLOWING table

SELECT feed.id, feed.username, feed.status, feed.timestamp, users.image 
FROM feed,users 
AND users.username = '$username'
ORDER BY feed.id DESC

Table design:

So how do I make this query include anyone from the FOLLOWING table?

TABLES:
--
-- Table structure for table `feed`
--

CREATE TABLE IF NOT EXISTS `feed` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `status` text NOT NULL,
  `ipaddress` varchar(100) NOT NULL,
  `timestamp` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

-- --------------------------------------------------------

--
-- Table structure for table `following`
--

CREATE TABLE IF NOT EXISTS `following` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `following` varchar(100) NOT NULL,
  `ipaddress` varchar(100) NOT NULL,
  `timestamp` int(11) NOT NULL,
  `isfollowing` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `user_level` int(11) NOT NULL DEFAULT '0',
  `name` varchar(100) NOT NULL,
  `image` varchar(100) NOT NULL DEFAULT 'default.png',
  `profile_background` varchar(255) NOT NULL DEFAULT 'default_background.png',
  `description` varchar(255) NOT NULL,
  `went_to_school` varchar(100) NOT NULL,
  `worked_at` varchar(100) NOT NULL,
  `lives_in` varchar(100) NOT NULL,
  `from_originally` varchar(100) NOT NULL,
  `expires` datetime NOT NULL,
  `ipaddress` varchar(50) NOT NULL,
  `browser` varchar(100) NOT NULL DEFAULT 'n/a',
  `show_email` int(11) NOT NULL DEFAULT '1',
  `timestamp` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;
7
  • You should be absolutely sure things like $username are escaped in your query or you could be in for bad times. Also why are you using the user name as a key here? It really should be based on something more immutable like ID if you're following proper database normalization practices. Using username exposes you to the risks of someone "inheriting" data from an account that was deleted if they choose the same name.
    – tadman
    Mar 22, 2016 at 18:35
  • My particular system doesnt allow username changes so a user will not be prone to inheritance. The $username var is escape in the php var so we are good there as well. Bad practice? Yes. I just got used to doing it in such a way :(
    – Lukeity
    Mar 22, 2016 at 18:39
  • It doesn't allow it today but you can't say for sure that won't happen in the future. Hopefully you can fix this before it becomes a huge issue. As a plus, because of their size ID-based indexes are generally way faster and more scalable.
    – tadman
    Mar 22, 2016 at 18:41
  • By the way, what you need here is a JOIN to combine the tables.
    – tadman
    Mar 22, 2016 at 18:42
  • Could you throw up a small example with the above query? I am kind of new to JOINing tables. Thanks man.
    – Lukeity
    Mar 22, 2016 at 18:45

1 Answer 1

0

The general idea here is to JOIN in the following table:

 SELECT feeds.*
   FROM feeds
   LEFT JOIN following ON following.following=feeds.username
   WHERE feeds.username=? OR following.username=?

The LEFT JOIN is a way of saying "combine A with B if there's a match, otherwise leave B as all NULL values". There's other types that do different things.

Note that this is extremely clunky using usernames as keys. I'd strongly encourage you to migrate to an ID based system as soon as possible.

You should also stay away from MyISAM tables, they don't support transactions and have no journal. A simple server crash can utterly trash the table beyond repair. InnoDB is significantly more robust and should be used whenever possible. In MySQL 5.7 there's rarely a reason to use MyISAM at all.

10
  • I need to obtain the user profile data (which is stored in users table) also. This doesn't account for unique folllowing (for instance if username1 is following 3 people, it will list 3 peoples * 3)
    – Lukeity
    Mar 22, 2016 at 18:59
  • You can join that in if you need to, but this will make your query increasingly slow. Why not just retrieve user data in a second query?
    – tadman
    Mar 22, 2016 at 19:01
  • I was told one query is always better. Maybe I'm wrong? :( Regardless on user_profile data, how would I get DISTINCT rows from an LEFT JOIN?
    – Lukeity
    Mar 22, 2016 at 19:03
  • One query can be better, but you need to evaluate it on a case by case basis. If you have a lot of user data you're pulling out then a second query will send the data once per user, not once per post. That could be significantly less data.
    – tadman
    Mar 22, 2016 at 19:04
  • Here's what I mean: i.imgur.com/svwXHm2.png - its showing 1 feed post 3 times based on 3 followers
    – Lukeity
    Mar 22, 2016 at 19:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.