简体   繁体   English

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

[英]what sql query statement should i use for a vb.net application?

I have three tables set up in a MySQL database called "event", "status" and "user". 我在名为“事件”,“状态”和“用户”的MySQL数据库中设置了三个表。 (as shown below: (如下所示:

EVENT TABLE (below) 活动表 (下) 活动表

STATUS TABLE (below) 状态表 (下) 状态表

USER TABLE (below) 用户表 (如下) 用户表

and when I get data from the event table I use the SQL query statement below to bind the persons first and last names together as one variable called "name" and then bind that name to the respective user_id; 当我从事件表中获取数据时,我使用下面的SQL查询语句将这些人的名字和姓氏作为一个名为“ name”的变量绑定在一起,然后将该名字绑定到相应的user_id上; and so on. 等等。 However when I make changes to the event table it doesn't show the changes I've made. 但是,当我对事件表进行更改时,它不会显示我所做的更改。 I'm certain it has something to do with the way I'm retrieving the data. 我敢肯定,这与我检索数据的方式有关。

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

So I am in need of a new SQL query statement that will take the information in those three tables and produce a data grid view in my vb.net program that will look similar to this: 因此,我需要一个新的SQL查询语句,该语句将获取这三个表中的信息,并在vb.net程序中生成一个数据网格视图,该视图类似于以下内容:

结果表

But will also accept any changes I make to the database through the data grid view in my vb.net program; 但也将接受我通过vb.net程序中的数据网格视图对数据库所做的任何更改; Or if my problem has nothing to do with the query statement, then I'd like to know how to fix this. 或者,如果我的问题与查询语句无关,那么我想知道如何解决此问题。

If you want to see the basic layout of my database (minus the personal information) then here is the script for my database. 如果要查看数据库的基本布局(减去个人信息),则这里是数据库的脚本。

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;

try this 尝试这个

 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

Please rewrite the query like so. 请这样重写查询。

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

However when I make changes to the event table it doesn't show the changes I've made. 但是,当我对事件表进行更改时,它不会显示我所做的更改。 I'm certain it has something to do with the way I'm retrieving the data 我确定这与我检索数据的方式有关

Why don't I see any changes I make to the event table? 为什么我看不到对event表所做的任何更改?

I think you want to show the output of the above query in a dataview and also enter data into that view. 我认为您想在数据视图中显示上述查询的输出,并在该视图中输入数据
That last thing is impossible for a view that is not fed by a anything other than a simple query like: 对于除了简单查询之类的其他视图之外的视图,最后的事情是不可能的:

SELECT * FROM table1   

The reason is that SQL has no idea how to place the data the tables used, nor does it want to know. 原因是SQL不知道如何放置所使用表的数据,也不知道。

If you make changes to the event table itself, you will see those of course when you rerun the query and if they meet the criteria in the select statement. 如果您对event表本身进行了更改,那么当您重新运行查询时如果它们符合select语句中的条件,您当然会看到这些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM