Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-31-2006, 05:43 PM   #1 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Setting a field equal to the row count of another table

Is there a way, in MySQL, to set a field in table "A" equal to a row count of another table where id = 'x'?

What I'm doing is building my own blog, more accurately the commenting system. I have a field in the table containing the blog information, which includes a field "num_comments". I'd like to be able to post comments without having to update both the comments table, and the blog table. I'm wondering if there is a specific function within MySQL that will allow me to do this?

While I'm here, how would I go about incrementing an int field without querying for the value first?
__________________
Current Project
Redline is offline   Reply With Quote
Old 04-02-2006, 10:05 PM   #2 (permalink)
l33t_1-1axx0r
Huzzah!
 
l33t_1-1axx0r's Avatar
 
Join Date: Feb 2006
Posts: 8
l33t_1-1axx0r is on a distinguished road
Send a message via AIM to l33t_1-1axx0r Send a message via MSN to l33t_1-1axx0r Send a message via Yahoo to l33t_1-1axx0r
Maybe I'm a little confused as to what you're asking, but it seems that the number of comments and the blog information is seperate... so you'd never need to update two tables at once. Instead, maybe have a field in the comments table that holds the ID of the blog it corresponds to...

A little more detail on how you're structuring may help.

As for the autoincrementing, just add NOT NULL AUTO_INCREMENT to the column when you create the tables.

e.g.

Code:
CREATE TABLE foo (
     id INT NOT NULL AUTO_INCREMENT,
     bar CHAR(30),
     PRIMARY KEY (id)
);
l33t_1-1axx0r is offline   Reply With Quote
Old 04-03-2006, 07:35 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
redhead is on a distinguished road
He's talking about the FOREIGN activation ie:
Code:
CREATE TABLE foo (
    id INT NOT NULL auto_increment,
    UNIQUE id (id),
    PRIMARY KEY (id)
);
CREATE TABLE bar (
    id INT NOT NULL auto_increment,
    foo_id INT NOT NULL,
    UNIQUE id (id),
    PRIMARY KEY (id)
    FOREIGN KEY (foo_id) REFERENCES foo
);
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table data from db as new form input tomycon PHP 2 03-03-2006 05:26 AM
Row Decrement Phantek Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 6 05-20-2005 10:59 AM
How can I count characters in a text field? jimmyoctane PHP 2 10-18-2003 03:38 PM


All times are GMT -8. The time now is 08:34 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting