我应该为vb.net应用程序使用什么sql查询语句?

时间:2011-08-31 14:34:29

标签: mysql sql vb.net

我在MySQL数据库中设置了三个表,分别名为“event”,“status”和“user”。 (如下图所示:

EVENT TABLE (下) Event Table

状态表(下) Status Table

USER TABLE (下) User Table

当我从事件表中获取数据时,我使用下面的SQL查询语句将人名和名字绑定在一起作为一个名为“name”的变量,然后将该名称绑定到相应的user_id;等等。但是,当我对事件表进行更改时,它不会显示我所做的更改。我确定它与我检索数据的方式有关。

SELECT CONCAT(u.lastname, ', ', u.firstname) AS Name
       , s.message AS Message
       , DATE_FORMAT(e.timestamp,'%b %d %Y - %r') AS DateTime
       , e.status AS Status 
FROM event e 
LEFT JOIN status s ON e.message_id = s.message_id
          , user u 
WHERE(e.user_id = u.user_id)
  AND event_id IN(
      SELECT MAX(e.event_id) FROM event e 
      GROUP BY e.user_id)
ORDER BY name

所以我需要一个新的SQL查询语句,它将获取这三个表中的信息并在我的vb.net程序中生成一个类似于此的数据网格视图:

Resulting Table

但是也会通过我的vb.net程序中的数据网格视图接受我对数据库所做的任何更改;或者,如果我的问题与查询语句无关,那么我想知道如何解决这个问题。

如果您想查看我的数据库的基本布局(减去个人信息),那么这里是我的数据库的脚本。

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `in_out`;
USE `in_out`;
CREATE TABLE `admin_levels` (
  `level_id` tinyint(3) unsigned NOT NULL auto_increment,
  `title` char(20) NOT NULL default '',
  PRIMARY KEY  (`level_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `event` (
  `event_id` mediumint(8) unsigned NOT NULL auto_increment,
  `user_id` smallint(5) unsigned NOT NULL default '0',
  `message_id` mediumint(8) unsigned default '0',
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `status` enum('In','Out') NOT NULL default 'In',
  `creator` smallint(5) unsigned default NULL,
  PRIMARY KEY  (`event_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `event` (`event_id`,`user_id`,`message_id`,`timestamp`,`status`,`creator`) VALUES 
 (1,1,1,'2005-01-17 11:50:00','Out',1),
 (2,2,1,'2005-01-17 11:57:00','Out',2),
 (3,3,1,'2005-01-17 11:59:00','Out',3),
 (4,1,3,'2005-01-17 13:30:00','In',1),
 (5,2,3,'2005-01-17 13:30:00','In',2),
 (6,3,3,'2005-01-17 13:30:00','In',3),
 (7,2,2,'2005-01-17 16:00:00','Out',2),
 (8,3,2,'2005-01-17 16:10:00','Out',3),
 (9,1,NULL,'2005-01-17 15:19:49','In',1);
CREATE TABLE `groups` (
  `group_name` char(20) NOT NULL default '',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  `scope` enum('Public','Private') default NULL,
  `deleted` enum('True','False') default NULL,
  PRIMARY KEY  (`group_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `status` (
  `message_id` mediumint(8) unsigned NOT NULL auto_increment,
  `user_id` smallint(5) unsigned default NULL,
  `message` char(255) NOT NULL default '',
  `deleted` enum('True','False') default NULL,
  PRIMARY KEY  (`message_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `status` (`message_id`,`user_id`,`message`,`deleted`) VALUES 
 (1,NULL,'Gone to Lunch','False'),
 (2,NULL,'Gone For The Day','False'),
 (3,NULL,'In Meeting','False');
CREATE TABLE `user` (
  `user_id` int(10) unsigned NOT NULL auto_increment,
  `lastname` char(40) NOT NULL default '',
  `firstname` char(40) NOT NULL default '',
  `phone` char(10) NOT NULL default '',
  `username` char(16) NOT NULL default '',
  `password` char(40) character set latin1 collate latin1_bin NOT NULL default '',
  `administrator` enum('TRUE','FALSE') NOT NULL default 'TRUE',
  `deleted` enum('TRUE','FALSE') NOT NULL default 'TRUE',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `user` (`user_id`,`lastname`,`firstname`,`phone`,`username`,`password`,`administrator`,`deleted`,`created`) VALUES 
 (1,'Hillyer','Mike','4033806535','mike','12345','TRUE','FALSE','2004-11-27 11:41:00'),
 (2,'Jones','Tom','4035551212','bob','54321','FALSE','FALSE','2005-01-17 13:52:00'),
 (3,'Johnson','Julie','4035551213','julie','weakpass','FALSE','FALSE','2005-01-17 13:55:00');
CREATE TABLE `user_group` (
  `group_name` char(20) NOT NULL default '',
  `user_id` smallint(5) unsigned NOT NULL default '0',
  `level_id` tinyint(3) unsigned default NULL,
  PRIMARY KEY  (`user_id`,`group_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

2 个答案:

答案 0 :(得分:1)

试试这个

 SELECT CONCAT(u.lastname + ', ' + u.firstname) AS Name
   , s.message AS Message
   , DATE_FORMAT(e.timestamp,'%b %d %Y - %r') AS DateTime
   , e.status AS Status 
FROM event e 
LEFT JOIN status s ON e.message_id = s.message_id inner join user u on e.user_id = u.user_id

WHERE event_id IN(
  SELECT MAX(e.event_id) FROM event e 
  GROUP BY e.user_id)
ORDER BY name

答案 1 :(得分:0)

请像这样重写查询。

SELECT CONCAT(u.lastname, ', ', u.firstname) AS Name
       , s.message AS Message
       , DATE_FORMAT(e.`timestamp`,'%b %d %Y - %r') AS DateTime
       , e.status AS Status 
FROM event e 
LEFT JOIN status s ON e.message_id = s.message_id
INNER JOIN user u ON (e.user_id = u.user_id)
WHERE e.event_id IN(
      SELECT MAX(e.event_id) FROM event e 
      GROUP BY e.user_id)
ORDER BY name
  

但是,当我对事件表进行更改时,它不会显示我所做的更改。我确定它与我检索数据的方式有关

为什么我看不到对event表所做的任何更改?

我认为您希望在数据视图中显示上述查询的输出,并且将数据输入该视图。
对于一个除了简单查询之外的任何东西都没有提供的视图,最后一件事是不可能的:

SELECT * FROM table1   

原因是SQL不知道如何将数据放在表中使用,也不知道。

如果您对event表本身进行了更改,那么当您重新运行查询时,如果它们符合select语句中的条件,您将会看到这些更改。