简体   繁体   中英

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". (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; 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:

结果表

But will also accept any changes I make to the database through the data grid view in my vb.net program; 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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