I've a massive problem creating a view in mySQL:
Table A in database DB1:
CREATE TABLE `a` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'internal ID',
`account` VARCHAR(10) NOT NULL DEFAULT '0',
`filename` VARCHAR(50) NOT NULL,
`filesize` BIGINT(15) NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
Table B in database DB2:
CREATE TABLE `b` (
`archive_id` INT(10) UNSIGNED NULL AUTO_INCREMENT,
`archive_datetime` DATETIME,
`id` INT(10) UNSIGNED NOT NULL,
`account` VARCHAR(10) NOT NULL DEFAULT '0',
`filename` VARCHAR(50) NOT NULL,
`filesize` BIGINT(15) NOT NULL DEFAULT '0'
PRIMARY KEY (`archive_id`)
)
ENGINE=Archive
Entries from table A are automatically transfered to table B via trigger if BEFORE DELETE.
I need a view that gives me all entries from table a and table b as if they were still in one table of the same database. Columns archive_id and archive_datetime can be ignored in the view as they are not needed for this scenario.
You could use UNION:
SELECT * FROM a UNION SELECT * FROM b;
You just have to replace * with the desired table columns.
SELECT id, account, filename, filesize FROM a UNION ALL SELECT id, account, filename, filesize FROM b
Surely I must be missing something?
Related
I have two tables:
parameters keeps all the para_ids and their names and is always updated to have all parameters in it.
CREATE TABLE `parameters` (
`para_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`para_id`),
UNIQUE KEY `idx_parameters_name` (`name`)
) ENGINE=InnoDB;
processing is holding a chunk of data every 5 minutes.
CREATE TABLE `processing` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`t_ns` bigint(20) unsigned NOT NULL DEFAULT '0',
`para_id` int(10) unsigned NOT NULL DEFAULT '0',
`value` varchar(1024) NOT NULL DEFAULT '',
`isanchor` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `data` (`para_id`,`t_ns`)
) ENGINE=InnoDB;
I want to keep a table actual_values with the last seen values that each parameter (if it occurred in processing) had. The para_ids are updated with an INSERT IGNORE before the update. Currently I have those queries:
INSERT IGNORE INTO actual_values (para_id) (SELECT DISTINCT para_id FROM parameters);
UPDATE actual_values a
JOIN processing p ON a.para_id = p.para_id
SET a.value = (SELECT p.value FROM processing p WHERE a.para_id = p.para_id ORDER BY t_ns DESC LIMIT 1);
I feel like this is not the optimal way to go, it takes quite long. Do you guys have better suggestions?
Table 1
CREATE TABLE IF NOT EXISTS `com_msg` (
`msg_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`msg_to` int(10) NOT NULL,
`msg_from` int(10) NOT NULL,
`msg_new` tinyint(1) unsigned NOT NULL DEFAULT '1',
`msg_content` varchar(300) NOT NULL,
`msg_date` date NOT NULL,
`bl_sender` tinyint(1) unsigned NOT NULL DEFAULT '0',
`bl_recip` tinyint(1) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`msg_id`),
UNIQUE KEY `msg_id` (`msg_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Table 2
CREATE TABLE IF NOT EXISTS `ac_vars` (
`user_id` int(10) unsigned NOT NULL,
`ac_ballance` smallint(3) unsigned NOT NULL DEFAULT '0',
`prof_views` mediumint(8) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`),
UNIQUE KEY `id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
When I use query :
SELECT ac_ballance, prof_views, COUNT( msg_id ) AS messages
FROM ac_vars
INNER JOIN com_msg ON user_id = msg_to
WHERE user_id =".$userid." AND com_msg.msg_new =1;
I get :
ac_ballance=NULL(incorrect)
prof_views=NULL(incorrect)
messages=0(correct)
But with Select statement just on ac_vars I get correct values, what is the correct way of doing this?
You want rows from the table ac_vars even when there's no corresponding row in the table com_msg.
So you must use a LEFT JOIN:
SELECT ac_ballance, prof_views, COUNT( msg_id ) AS messages
FROM ac_vars
LEFT JOIN com_msg
ON user_id = msg_to AND com_msg.msg_new =1
WHERE user_id =".$userid.";
Please note that the condition
com_msg.msg_new =1
got to be a part of the JOIN condition and not the WHERE clause, because there's no value in com_msg that fulfills this condition.
Note
Adding
GROUP BY ac_ballance, prof_views
is not needed by MySQLs optimization because the values in those columns are directly dependent of the user_id and the WHERE clause permits only one single row.
I know that I can reset the primary key in this way:
ALTER TABLE `users` DROP `id`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
But I'm wondering how I can adapt this to apply the primary key according to the timestamp in my timestamp column, i.e. the row with the oldest timestamp gets the value 1, the next oldest, the value 2, etc.
OUTPUT of SHOW CREATE TABLE
CREATE TABLE `tracks` (
`id` int(10) unsigned NOT NULL auto_increment,
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
`from_user` varchar(50) NOT NULL,
`source_filename` varchar(80) NOT NULL,
`uploaded_page` varchar(50) NOT NULL,
`operating_system` varchar(50) NOT NULL,
`browser` varchar(50) NOT NULL,
`os_browser_version` varchar(200) NOT NULL,
`title` varchar(100) NOT NULL,
`artist` varchar(60) NOT NULL,
`album` varchar(120) NOT NULL,
`genre` varchar(120) NOT NULL,
`format` varchar(10) NOT NULL,
`bitrate` mediumint(9) NOT NULL,
`conversion_needed` tinyint(1) NOT NULL COMMENT 'if not mp3, or higher than 192kbps',
`conversion_successful` tinyint(1) NOT NULL,
`art_extracted` tinyint(1) NOT NULL,
`art_location` varchar(200) NOT NULL,
`file_location` varchar(200) NOT NULL,
`status` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=96 DEFAULT CHARSET=utf8
Create a new table called users2 that has the same structure as users:
CREATE TABLE users2 LIKE users;
Then run this statment:
INSERT INTO users2
(col1, col2, ... coln)
SELECT col1, col2, ... coln
FROM users
ORDER BY your_timestamp
For your specific table:
INSERT INTO tracks2
(`timestamp`,`from_user`,`source_filename`,`uploaded_page`,`operating_system`,`browser`,`os_browser_version`,`title`,`artist`,`album`,`genre`,`format`,`bitrate`,`conversion_needed`,`conversion_successful`,`art_extracted`,`art_location`,`file_location`,`status`)
SELECT `timestamp`,`from_user`,`source_filename`,`uploaded_page`,`operating_system`,`browser`,`os_browser_version`,`title`,`artist`,`album`,`genre`,`format`,`bitrate`,`conversion_needed`,`conversion_successful`,`art_extracted`,`art_location`,`file_location`,`status`
FROM tracks
ORDER BY timestamp
I know you've already accepted an answer, but for the sake of posterity I want to chime in with a simpler solution. MySQL allows you to re-order the rows of a table using ALTER TABLE...ORDER BY..., so you can easily do this by adding one more ALTER TABLE statement to your original example:
ALTER TABLE `users` DROP COLUMN `id`;
ALTER TABLE `users` ORDER BY `timestamp`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD COLUMN `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
I encountered this quite a few times so far, but still don't understand it (my MySQL internals skills are equal to none).
I know it's probably a PEBKAC but trying to replicate the behavior manually ends up with an error (autoincrement).
CREATE TABLE `foo_bar` (
`id` int(12) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(12) unsigned DEFAULT NULL,
`order_id` int(12) unsigned DEFAULT NULL,
`email_address` varchar(50) DEFAULT NULL,
`mobile_number` varchar(20) DEFAULT NULL,
`message` longtext NOT NULL,
`message_received` int(12) unsigned DEFAULT NULL,
`failed_to_send` tinyint(1) unsigned DEFAULT NULL,
`fraudulent_activity` tinyint(1) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=ARCHIVE DEFAULT CHARSET=utf8;
When your program inserts a row in the database, it should provide NULL as the value for auto-incremented field:
CREATE TABLE `customers` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 128 ) NOT NULL
) ENGINE = MYISAM ;
INSERT INTO `customers` ( `id` , `name` )
VALUES ( NULL , 'Customer 1' ), ( NULL , 'Customer 2' );
If you try to insert a specific value in id field, MySQL will give an error:
SQL query:
INSERT INTO `customers` ( `id` , `name` )
VALUES ( '1', 'Customer 3' );
MySQL said:
#1062 - Duplicate entry '1' for key 'PRIMARY'
Although the answer to "what caused this" didn't come up, REPAIR TABLE fixes the problem.
Answering this so I can close the question.
I want to create a rating system that keeps track of who voted for which article, and how many votes that article received along with the value of each vote.
And I was wondering what is the best way to go about this?
Here is what I have so far below.
CREATE TABLE IF NOT EXISTS `vote` (
`counter` int(8) NOT NULL default '0',
`value` int(8) NOT NULL default '0'
)
CREATE TABLE articles (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`title` TEXT NOT NULL,
`summary` TEXT DEFAULT NULL,
`content` LONGTEXT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS `users` (
`id` int(8) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
)
You need to keep track of your users somewhere
CREATE TABLE IF NOT EXISTS `vote` (
`user_id` int(8) NOT NULL,
`article_id` int(8) NOT NULL,
`value` int(8) default 0,
`datetime` timestamp DEFAULT CURRENT_TIMESTAMP
)
(Indexes are missing here and for the other tables)
If you want to unnormalize the number of votes, it should then be put in the articles table but if you do not have too much records, I would rather advice to compute it using a regular count and put the front in cache (refreshed each time there is a new vote for the article of course).