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

  • SEARCH
  • Home
  • 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 7166281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:23:18+00:00 2026-05-28T14:23:18+00:00

I have 3 InnoDB tables: emails , websites and subscriptions . emails table has

  • 0

I have 3 InnoDB tables: emails, websites and subscriptions.

emails table has id and email columns.

websites table has id and address columns.

subscriptions table has id, email_id and website_id columns.

What I’m tring to do is supply an email and return a table with columns address and subscribed. The former is a list of all the addresses in the websites table and the latter gets value 1 if the supplied email address has an occurence in the subscriptions table with website_id set to that website, or 0 otherwise. But I’m willing to retain all the websites even if the user is not found.

The point I’m stuck is where I should change the value of the virtual column subscribed from 0 to 1 when that email has that record.

Here’s my query so far. Does anybody know how to do this?

SELECT `address`, "0" AS `subscribed`
/* 0 becomes 1 for the websites email has subscribed to */
FROM `websites` a
LEFT JOIN (
    SELECT s.`website_id` FROM `subscriptions` s
    RIGHT JOIN (
        SELECT `id` AS `email_id` FROM `emails`
        WHERE `email`='someone@mail.com' LIMIT 1) e
    ON s.`email_id`=e.`email_id`) l
ON l.`website_id`=a.`id`

And here are the example outputs for the desired values for the subscribed column:

  • If email is not found in the emails table all the rows get value 0
  • If email is found in the emails table…
    • if it is not found in subscriptions table all the rows get value 0
    • if it is found in subscriptions table, the appropriate address rows get value 1

Let me know if I couldn’t wxplain it well. Does anytbody know what I should alter in my query?

Thanks in advance.

  • 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-05-28T14:23:19+00:00Added an answer on May 28, 2026 at 2:23 pm
    SELECT w.address, CASE WHEN s.id IS NULL THEN 0 ELSE 1 END AS subscribed
        FROM websites w
            LEFT JOIN subscriptions s
                INNER JOIN emails e
                    ON s.email_id = e.id
                        AND e.email = 'someone@mail.com'
                ON w.id = s.website_id
    

    You could also come up with the subscribed value this way, which is a bit more concise but also somewhat less obvious.

    SELECT w.address, COALESCE(s.id/s.id, 0) AS subscribed
        FROM websites w
            LEFT JOIN subscriptions s
                INNER JOIN emails e
                    ON s.email_id = e.id
                        AND e.email = 'someone@mail.com'
                ON w.id = s.website_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL InnoDB table with 238 columns. 56 of them are TEXT
I have two InnoDB tables: CREATE TABLE master( id INTEGER UNSIGNED NOT NULL auto_increment
I have two tables named MEMBER - columns id(primary key), name, email & TOPICS
I have an PHP/MySQL eCommerce application that has a few innoDB tables (for the
I'm using MySQL, all my tables are using InnoDB engine. I have some columns
I have the following query (all tables are innoDB) INSERT INTO busy_machines(machine) SELECT machine
I have a innoDB table which records online users. It gets updated on every
I have a MySQL-InnoDB table with 350,000+ rows, containing a couple of things like
I have a MySQL InnoDB table on a RedHat Enterprise Linux 4 server, and
I have a MySQL InnoDB table with a status column. The status can be

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.