Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8599977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:30:52+00:00 2026-06-12T01:30:52+00:00

How can I query for all records in a table called photos, and know

  • 0

How can I query for all records in a table called photos, and know which of the resulting photos have been bookmarked by the current user using a single query?

Here are my tables:

--
-- Table structure for table `photos`
--

CREATE TABLE IF NOT EXISTS `photos` (
  `id` int(11) NOT NULL auto_increment,
  `author` bigint(20) NOT NULL COMMENT 'The user''s Facebook ID.',
  `filename` varchar(255) NOT NULL,
  `thumbnail` varchar(255) NOT NULL,
  `post_date` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `description` varchar(140) NOT NULL,
  `finalist` tinyint(4) NOT NULL DEFAULT '0',
  CONSTRAINT user_must_exist FOREIGN KEY (author)
    REFERENCES users(facebook_id)
    ON DELETE CASCADE,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

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

--
-- Table structure for table `bookmarks`
--

CREATE TABLE IF NOT EXISTS `bookmarks` (
  `facebook_id` bigint(20) NOT NULL COMMENT 'The author''s Facebook ID.',
  `photo_id` int(11) NOT NULL,
  CONSTRAINT photo_should_exist FOREIGN KEY (photo_id)
    REFERENCES photos(id)
    ON DELETE CASCADE,
  CONSTRAINT user_should_exist FOREIGN KEY (facebook_id)
    REFERENCES users(facebook_id)
    ON DELETE CASCADE,
  UNIQUE KEY `no_duplicates` (`facebook_id`,`photo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='The user''s favourite photos.';

I would imagine this query would look something like the following:

SELECT 
    photos.*, 
    bookmarks.photo_id AS bookmark 
FROM photos
LEFT JOIN bookmarks 
    ON bookmarks.photo_id = photos.id 
    AND photos.author = 123456789

However this doesn’t work and I receive the following MySQL error:

Unknown column 'photos.id' in 'on clause'

keyur’s code worked for me after a minor typo fix which Barmar pointed out.

SELECT photos . * , bookmarks.photo_id AS bookmark
FROM photos
LEFT JOIN bookmarks ON photos.id = bookmarks.photo_id
AND bookmarks.facebook_id = 123456789

Thank you.

Editted for typo.

Editted for second typo.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:30:54+00:00Added an answer on June 12, 2026 at 1:30 am

    you query should be like this.

    SELECT 
        photos.*, 
        bookmarks.id AS bookmark 
    FROM photos
    LEFT JOIN bookmarks 
        ON photos.id = bookmarks.photo_id 
        AND bookmarks.facebook_id = 123456789
    

    according your table structure photos.facebook_id is not exist; it is in bookmarks table

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a table i can query all of Oracle Reserved Words? Example, i
I have some query. First of all let's have an iPhone application flow which
I have tables called: activities and deficiencies . activities table contains all activities registered
I have a relationship table in a rails application called edit_privileges , in which
I have a successful query that currently returns all data except for one table.
I can't figure this out. This query inserts a new record with all the
Iam using windows forms. How can I query all child controls of a Form
I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
How can I order by descending my query set in django by date? Reserved.objects.all().filter(client=client_id).order_by('check_in')
How can i make a SQL query from actionscript? and render all the results

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.