LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2009, 03:29 AM   #1
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Rep: Reputation: 31
mysql join


This is an easier (?) alternative:
what if i had a table and i wanted two sets of numbers that are like auto_increment; how would i auto-increment numbers starting from 1 going down from where tbl2.x = 'something', and then do another one starting from 1 where tbl2.x = 'something_else'



this is the original question:

I can't even describe my problem succinctly in the subject of this thread.

Here's what I'm trying to do:
select id from db2.tbl2;
returns:
id
1
2
3
...
(this query already works properly)

select tbl2_id from db1.tbl1 where db1.tbl1.table = 'db2.tbl2';
returns:
same as above.

The problem now is that the second query returns:
tbl2_id
1
1
1
...

I filled out the column tbl2_id by:

update db1.tbl1, db2.tbl2 set db1.tbl1.tbl2_id = db2.tbl2.id where db1.tbl1.table = 'db2.tbl2';
OR
update db1.tbl1 join db2.tbl2 ON (db1.tbl1.table = 'db2.tbl2') set db1.tbl1.tbl2_id = db2.tbl2.id;

Both yield the same result.

Have any idea how to do what I want to do?

Last edited by secretlydead; 10-12-2009 at 03:48 AM.
 
Old 10-12-2009, 08:24 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Please describe your problem more fully. Give us details.

"Auto-increment" fields are really just designed for providing primary-key values... when you need for a row to have a primary key but you really don't care what the value is. (That is to say, "only the computer cares what it is ... not the end-users or the business.)

If the numbers are important, use a separate table of counters ("next_invoice_number", etc...). First you acquire the next number using this table, then you increment the counter, then you use the number when inserting rows into other tables.
 
Old 10-12-2009, 08:14 PM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
This won't work, try to give us this:

Code:
table1:
-------------------------------
| id | value | something more?|
-------------------------------
| 1  | foo   |                |
| 2  | bar   |                |
| 3  | baz   |                |
------------------------------

table2:
-------------------------------
| id | value | something more?|
-------------------------------
| 1  | golf  |                |
| 2  | soccer|                |
| 3  | swim  |                |
------------------------------
And then give an example of what you want your query to be. My tables are nonsense of course, we need yours


jlinkels
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't join Windows 2000 domain using net ads join The Cat Linux - Networking 2 09-23-2008 11:41 AM
Unable to join domain using Net Join command in FC3 client jeb083079 Linux - Networking 9 07-30-2007 02:41 AM
MySQL: Help with using join to simulate subquery MicahCarrick Programming 1 09-30-2006 07:17 AM
LXer: Learn how to join tables in MySQL LXer Syndicated Linux News 0 08-08-2006 03:21 PM
MySQL Join Syntax Issue PerfectReign Programming 5 05-11-2006 08:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration