Returning column indicating row existence in another table

sqlite3

I have two tables, Table1 and Table2.

Both of these tables have a column called Name.

I would like to query Table2 and return the Name column. In addition, I would like to return a column containing the values 1 or 0 depending on whether any rows in Table1 also contain the same value for Name.

What is the most efficient way to do this?

I'm looking for something like:

SELECT
  Name, IF EXISTS (SELECT * FROM Table1 WHERE Table1.Name = Table2.Name) 1 ELSE 0
FROM
  Table2

Answers


Sorry, I'm not very familiar with SQLite, but this should work for you. The trick is a Left Outer Join and comparison on the joined table.

SELECT
    Table2.Name,
    CASE WHEN Table1.Name IS NOT NULL THEN 1 ELSE 0 END
FROM
    Table2
LEFT OUTER JOIN
    Table1 ON Table1.Name = Table2.Name

I'm not so sure the CASE syntax is the best but if you are more familiar with conditionals in SQLite you can clean that part up.


Need Your Help

How can I center multiple divs on a page, and left-align them when a div drops to next row?

jquery css html centering

I have a page with several "modules" made up of individual <div>s that are 159 px wide and 160 px high. The modules should form a centered row. The containing <div> is set to 70% width....

Gmail recording with Jmeter

security ssl gmail jmeter recording

I would like to capture gmail with jmeter. so i am trying to record the script. but once i have started jmeter script and open the gmail in browser. i am getting below exception in browser.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.