Mailing List
Home
Forum Home
MySQL General - General MySQL discussion
MaxDB - Everything about MaxDB, formerly known as SAP DB
MySQL++ - Programming with the C++ API to MySQL
MySQL on Win32 - Runing MySQL on Windows 9x/Me/NT/2000/XP
ODBC - ODBC with the MySQL Connector/ODBC driver
Java Help - Mostly related to the MySQL Connector/J driver
Perl - Perl support for MySQL with DBI and DBD::mysql
GUI - MySQL GUI Tools
Announcement
Subjects
mysql openssl Question
mysql:it 's a db not a dbms how it 's possible?!
Does the binary log enabling affect the MySQL performances?
Strange behavior, Table Level Permission
FULLTEXT query format question
Preventing Duplicate Entries
Comparing and writing out BLOBS
Executing MySQL Commands From Within C Program
Can 't access mysql after kernel upgrade
Mysql 4 0 Oracle Stored Procedure Trigger Conversion
Downgrade Mysql from 4 to 3 23
MySQL Cluster Software
mysql test 4 1 fails with the gis test
ERROR 2002: Can 't connect to local MySQL server through socket
Getting Identity after INSERT
Update one field with more fields from another table
ERROR 1045: Access denied for user: 'root@localhost ' (Using
password: NO)
mysql have same function mthod as Oracle decode()
 
SELECT ALL and flag [solved]

SELECT ALL and flag [solved]

2006-06-01       - By Peter Lauri

 Back
Solved it by:

SELECT table_a.name , if( table_a.id = table_b.table_a_id, 1, 0 ) AS
theindicator
FROM table_a
LEFT OUTER JOIN table_b ON ( table_a.id = table_b.table_a_id )

/Peter


-- ---- ----

Hi,

I have a table table_a and table_b:

table_a {
id
name
}

table_b {
table_a_id
b_value
}

Table A is a table with names, and table B is a table with values for a
specific name (optional, therefore a specific table).

I would like to select all records in A, done by:

SELECT name FROM table_a;

Returns:
Peter
Johan
Fredrik

But then I also would like to have a flag that flags if table_b, I want it
to return this if Peters id is the only one matching in table_b:

Peter     1
Johan     0
Fredrik   0

I tried:

SELECT name, if(table_a.id=table_b.table_a_id, 1, 0) AS indid FROM table_a
JOIN table_b;

But that generates multiple rows of the records in table_a. I tried GROUP BY
in combination with ORDER BY, but I did not manage to get it to work.

How can I do that if() without having to action do a join, I just want to
check if it exist, and then give value 1 or 0.

Anyone with ideas?

/Peter


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=lists@(protected)


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/mysql?unsub=mysql@(protected)