473,388 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

MYSQL select question

Hi,

I was wondering if anyone can help me with this query. I have two tables.
table_1 is a master table that contains all records. table_2 contains IDs of
some records from table_1 and flags from those records. I'd like to query
all records and set the flags for them with following test: if a record
exists in table_2, set its flag from FLAG col. else set flag to '0'. return
all flags as col MY_FLAG. Below is the structure and a query I have so far.

Many thanks for any help,

Dave

table_1
ID NAME
-------- -----------------
10001 10001 name
10002 10002 name
10003 10002 name

table_2
ID FLAG
-------- -------
10001 1
10002 0

desired result:
ID NAME MY_FLAG
10001 10001 name 1
10002 10002 name 0
10003 10002 name 0

The following would give me an ERROR 1109: Unknown table 'table_2' in field
list

SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0)
AS MY_FLAG
FROM table_1
WHERE table_1.ID LIKE '1000%' ORDER BY table_1.ID;

if I try the following query, it returns incorrect cols for rows:
SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0)
AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' ORDER BY table_1.ID;

returns:
ID NAME MY_FLAG
10001 10001 name 1
10001 10001 name 0
10002 10002 name 0
10003 10002 name 0

Jul 17 '05 #1
4 1432
Dave wrote:
Hi,

[snipped]

if I try the following query, it returns incorrect cols for rows:
SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0)
AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' ORDER BY table_1.ID;

returns:
ID NAME MY_FLAG
10001 10001 name 1
10001 10001 name 0
10002 10002 name 0
10003 10002 name 0


SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0)
AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' ORDER BY table_1.ID
and table_1.ID = table_2.ID;

MfG

Geoff.

--
Unofficial F1 Database: http://glibs.ssmmdd.co.uk/
Update: 22nd May, 2005
USENET Email address is a spam trap, send Emails to address in the DB
Jul 17 '05 #2
Thanks Geoff,

Did you mean:
SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0)
AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' and table_1.ID = table_2.ID
ORDER BY table_1.ID;

If so this works but won't return the row:
10003 10002 name 0

As the query is restricted by matching ID cols in both tables.

Thanks,

Dave

"Geoff May" <Be***********@t-online.de> wrote in message
news:d6*************@news.t-online.com...
Dave wrote:
Hi,

[snipped]

if I try the following query, it returns incorrect cols for rows:
SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0) AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' ORDER BY table_1.ID;

returns:
ID NAME MY_FLAG
10001 10001 name 1
10001 10001 name 0
10002 10002 name 0
10003 10002 name 0
SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG,

0) AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' ORDER BY table_1.ID
and table_1.ID = table_2.ID;

MfG

Geoff.

--
Unofficial F1 Database: http://glibs.ssmmdd.co.uk/
Update: 22nd May, 2005
USENET Email address is a spam trap, send Emails to address in the DB

Jul 17 '05 #3
Dave wrote:
Thanks Geoff,

Did you mean:
SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0)
AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' and table_1.ID = table_2.ID
ORDER BY table_1.ID;

If so this works but won't return the row:
10003 10002 name 0

As the query is restricted by matching ID cols in both tables.


Then you need to use the JOIN, something like this:

SELECT table_1.ID, table_1.NAME, if(table_1.ID = table_2.ID,
table_2.FLAG, 0)
FROM table_1
left left join table_2 on (table_1.ID = table_2.ID)
where table_1.ID LIKE '1000%'
ORDER BY table_1.ID;

MfG

Geoff.

--
Unofficial F1 Database: http://glibs.ssmmdd.co.uk/
Update: 22nd May, 2005
USENET Email address is a spam trap, send Emails to address in the DB
Jul 17 '05 #4
The query below worked as expected. Geoff, thanks a lot!

SELECT
table_1.ID,
table_1.NAME,
IF(table_1.ID = table_2.ID, table_2.FLAG, 0)
FROM table_1
LEFT JOIN table_2 on (table_1.ID = table_2.ID)
WHERE table_1.ID LIKE '1000%'
ORDER BY table_1.ID;
"Geoff May" <Be***********@t-online.de> wrote in message
news:d6*************@news.t-online.com...
Dave wrote:
Thanks Geoff,

Did you mean:
SELECT table_1.ID, table_1.NAME if(table_1.ID = table_2.ID, table_2.FLAG, 0) AS MY_FLAG
FROM table_1, table_2
WHERE table_1.ID LIKE '1000%' and table_1.ID = table_2.ID
ORDER BY table_1.ID;

If so this works but won't return the row:
10003 10002 name 0

As the query is restricted by matching ID cols in both tables.


Then you need to use the JOIN, something like this:

SELECT table_1.ID, table_1.NAME, if(table_1.ID = table_2.ID,
table_2.FLAG, 0)
FROM table_1
left left join table_2 on (table_1.ID = table_2.ID)
where table_1.ID LIKE '1000%'
ORDER BY table_1.ID;

MfG

Geoff.

--
Unofficial F1 Database: http://glibs.ssmmdd.co.uk/
Update: 22nd May, 2005
USENET Email address is a spam trap, send Emails to address in the DB

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
19
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5...
8
by: Tony Clarke | last post by:
Hi, Just a quick question about performance with MySQL & PHP. If I had a table in a MySQL database with about 100,000 records in it and I need to find the last record is there a quick way to do...
1
by: Marcus | last post by:
Hello, quick question about MySQL storing NULL values... Say I have a textbox called $_POST and a variable $var. if(empty($_POST)) $var = NULL; else $var = $_POST; Disregarding...
23
by: phpfrizzle | last post by:
Hi there, I have a site with products on it. The site has a mysql backend. All products belong to certain series (table series). There can be up to 4 different products (table products)...
51
by: w_curtis | last post by:
I'm an Access user, and I'm trying to learn MySQL and then PHP so I can make some web databases. But it just isn't clicking. I've followed some tutorials, and picked up a book, but just getting...
0
by: jy2003 | last post by:
Below is what I got by using MySQL's --log startup option. The original Java(with JDBC driver) program that creates the queries at runtime was coded by other people, and unfortunately, they are not...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
6
by: ojorus | last post by:
Hi! My company make several flash-based games, and I use php to communicate with mysql to provide highscore-lists. My problem is this: When I save a player's score in the mysql-table, I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.