I'm working on an audit log report and the users who like to see the original values that were inserted when the record was created - but unfortunately our audit logs simply store the record as "Record Created" but the record does not contain the inserted values. See screenshot below:
So all I originally know is that this certification was inserted on 3/14/2019 - not ideal for what I'm trying to do but it is what it is.
I am interested in finding the inserted values for four columns from this table:
CertificationTypeID
CertificateIdentifier
StateCertificationIssued
DateCertificationIssued
Using the audit logs and the certifications table, I am able to reconstruct what the original values were when the record was inserted - but each of the columns from the certification table are represented by four columns in my audit log record set:
OriginalValue
FieldName
ActionDate
RecordCreation
What I am hoping to accomplish is to unpivot ( or cross apply ) the columns that contain the original value records into a rows that would represent each column that was inserted.
For example, the first four columns for the original records are CT_OriginalValue, CT_FieldName, CT_ActionDate, and CT_RecordCreation. I need those to become four rows that would replace the first "Record Created" that did not contain any information.
Ideally - CT_FieldName & CT_RecordCreation would be concantated and unpivot to a row and fall under ColumnName, CT_OriginalValue would unpivot to the NewValue column, CT_ActionDate would unpivot to DateChanged.
I have tried to UNPIVOT but I'm not having much luck and I've seen CROSS APPLY used in these situations but I cannot figure out how to apply to this situation.
Sample Data below:
CREATE TABLE #AuditLogCustomerCert
(
ColumnName NVARCHAR (128) ,
OldValue VARCHAR (MAX) ,
NewValue VARCHAR (MAX) ,
DateChanged DATETIME2 (7) ,
ActionType CHAR (1) ,
AuditLogPK VARCHAR (400) ,
CT_OriginalValue VARCHAR (200) ,
CT_FieldName VARCHAR (19) ,
CT_ActionDate DATETIME2 (7) ,
CT_RecordCreation VARCHAR (14) ,
CI_OriginalValue VARCHAR (MAX) ,
CI_FieldName VARCHAR (21) ,
CI_ActionDate DATETIME2 (7) ,
CI_RecordCreation VARCHAR (14) ,
SCI_OriginalValue VARCHAR (MAX) ,
SCI_FieldName VARCHAR (24) ,
SCI_ActionDate DATETIME2 (7) ,
SCI_RecordCreation VARCHAR (14) ,
DCI_OriginalValue DATETIME ,
DCI_FieldName VARCHAR (23) ,
DCI_ActionDate DATETIME2 (7) ,
DCI_RecordCreation VARCHAR (14)
);
INSERT INTO #AuditLogCustomerCert
VALUES ( N'Record Created', NULL, NULL, N'2019-03-14T10:55:06.0014212', 'I', '<CustomerCertificationID=327857>', 'Hunter Safety Certificate', 'CertificationTypeID', N'2019-03-14T10:55:06.0014212', 'Record Created', 'IA299038794' ,
'CertificateIdentifier' , N'2019-03-14T10:55:06.0014212', 'Record Created', '66', 'StateCertificationIssued', N'2019-03-14T10:55:06.0014212', 'Record Created', N'2019-02-28T00:00:00', 'DateCertificationIssued' ,
N'2019-03-14T10:55:06.0014212' , 'Record Created' ) ,
( N'StateCertificationIssued', '66', 'UA', N'2019-03-14T13:04:45.1401862', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 14 2019 10:55AM', 'Mar 19 2019 1:52PM', N'2019-03-19T13:52:29.5221885', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 19 2019 1:52PM', 'Mar 22 2019 10:43AM', N'2019-03-22T10:43:42.1068855', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 22 2019 10:43AM', 'Mar 22 2019 2:38PM', N'2019-03-22T14:38:57.5266904', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'StateCertificationIssued', 'UA', 'NJ', N'2019-03-26T11:25:28.9015254', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'CountryCertificationIssued', 'Gabon ', 'United States ', N'2019-03-26T11:25:28.9015254', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'createDate', 'Mar 22 2019 2:38PM', 'Mar 26 2019 2:23PM', N'2019-03-26T14:23:27.8660009', 'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'Record Created', NULL, NULL, N'2019-03-26T10:30:19.3982917', 'I', '<CustomerCertificationID=327877>', 'Boater Safety Certificate', 'CertificationTypeID', N'2019-03-26T10:30:19.3982917', 'Record Created', 'IAC031900001' ,
'CertificateIdentifier' , N'2019-03-26T10:30:19.3982917', 'Record Created', 'IA', 'StateCertificationIssued', N'2019-03-26T10:30:19.3982917', 'Record Created', N'2019-03-07T00:00:00', 'DateCertificationIssued' ,
N'2019-03-26T10:30:19.3982917' , 'Record Created' ) ,
( N'CertificateIdentifier', 'IAC031900001', 'IAC031900002', N'2019-03-26T22:02:21.3292077', 'U', '<CustomerCertificationID=327877>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) ,
( N'Record Created', NULL, NULL, N'2019-03-26T22:12:29.4201277', 'I', '<CustomerCertificationID=327887>', 'Temporarily Disabled', 'CertificationTypeID', N'2019-03-26T22:12:29.4201277', 'Record Created', '', 'CertificateIdentifier' ,
N'2019-03-26T22:12:29.4201277' , 'Record Created', 'IA', 'StateCertificationIssued', N'2019-03-26T22:12:29.4201277', 'Record Created', N'2019-03-13T00:00:00', 'DateCertificationIssued', N'2019-03-26T22:12:29.4201277' ,
'Record Created' ) ,
( N'CertificateIdentifier', '', 'IAC9985478', N'2019-03-26T22:12:47.3471693', 'U', '<CustomerCertificationID=327887>', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
SELECT * FROM #AuditLogCustomerCert;
--SELECT ColumnName, up.NewValue
--FROM (
--SELECT CONCAT(ColumnName,'-',CT_FieldName), CT_OriginalValue
--FROM #AuditLogCustomerCert
--) AS cp
--UNPIVOT
--(
--NewValue FOR NewValue IN ( CT_OriginalValue )) AS up;
DROP TABLE IF EXISTS #AuditLogCustomerCert;
What about the "UNPIVOT" that you need, and "UNION ALL" with others? And I did not care about the order of the rows.
IF OBJECT_ID('tempdb..#AuditLogCustomerCert') IS NOT NULL
DROP TABLE #AuditLogCustomerCert;
CREATE TABLE #AuditLogCustomerCert(
ColumnName NVARCHAR (128) ,
OldValue VARCHAR (MAX) ,
NewValue VARCHAR (MAX) ,
DateChanged DATETIME2 (7) ,
ActionType CHAR (1) ,
AuditLogPK VARCHAR (400) ,
CT_OriginalValue VARCHAR (200) ,
CT_FieldName VARCHAR (19) ,
CT_ActionDate DATETIME2 (7) ,
CT_RecordCreation VARCHAR (14) ,
CI_OriginalValue VARCHAR (MAX) ,
CI_FieldName VARCHAR (21) ,
CI_ActionDate DATETIME2 (7) ,
CI_RecordCreation VARCHAR (14) ,
SCI_OriginalValue VARCHAR (MAX) ,
SCI_FieldName VARCHAR (24) ,
SCI_ActionDate DATETIME2 (7) ,
SCI_RecordCreation VARCHAR (14) ,
DCI_OriginalValue DATETIME ,
DCI_FieldName VARCHAR (23) ,
DCI_ActionDate DATETIME2 (7) ,
DCI_RecordCreation VARCHAR (14)
);
INSERT #AuditLogCustomerCert
VALUES (N'Record Created', NULL, NULL, N'2019-03-14T10:55:06.0014212', 'I',
'<CustomerCertificationID=327857>', 'Hunter Safety Certificate',
'CertificationTypeID', N'2019-03-14T10:55:06.0014212',
'Record Created', 'IA299038794', 'CertificateIdentifier',
N'2019-03-14T10:55:06.0014212', 'Record Created', '66',
'StateCertificationIssued', N'2019-03-14T10:55:06.0014212',
'Record Created', N'2019-02-28T00:00:00', 'DateCertificationIssued',
N'2019-03-14T10:55:06.0014212', 'Record Created'),
(N'StateCertificationIssued', '66', 'UA', N'2019-03-14T13:04:45.1401862',
'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) ,
(N'createDate', 'Mar 14 2019 10:55AM', 'Mar 19 2019 1:52PM',
N'2019-03-19T13:52:29.5221885', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'createDate', 'Mar 19 2019 1:52PM', 'Mar 22 2019 10:43AM',
N'2019-03-22T10:43:42.1068855', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'createDate', 'Mar 22 2019 10:43AM', 'Mar 22 2019 2:38PM',
N'2019-03-22T14:38:57.5266904', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'StateCertificationIssued', 'UA', 'NJ', N'2019-03-26T11:25:28.9015254',
'U', '<CustomerCertificationID=327857>', NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(N'CountryCertificationIssued', 'Gabon ', 'United States ',
N'2019-03-26T11:25:28.9015254', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'createDate', 'Mar 22 2019 2:38PM', 'Mar 26 2019 2:23PM',
N'2019-03-26T14:23:27.8660009', 'U', '<CustomerCertificationID=327857>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'Record Created', NULL, NULL, N'2019-03-26T10:30:19.3982917', 'I',
'<CustomerCertificationID=327877>', 'Boater Safety Certificate',
'CertificationTypeID', N'2019-03-26T10:30:19.3982917', 'Record Created',
'IAC031900001', 'CertificateIdentifier' , N'2019-03-26T10:30:19.3982917',
'Record Created', 'IA', 'StateCertificationIssued',
N'2019-03-26T10:30:19.3982917', 'Record Created', N'2019-03-07T00:00:00',
'DateCertificationIssued', N'2019-03-26T10:30:19.3982917',
'Record Created'),
(N'CertificateIdentifier', 'IAC031900001', 'IAC031900002',
N'2019-03-26T22:02:21.3292077', 'U', '<CustomerCertificationID=327877>',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL),
(N'Record Created', NULL, NULL, N'2019-03-26T22:12:29.4201277', 'I',
'<CustomerCertificationID=327887>', 'Temporarily Disabled',
'CertificationTypeID', N'2019-03-26T22:12:29.4201277', 'Record Created',
'', 'CertificateIdentifier', N'2019-03-26T22:12:29.4201277',
'Record Created', 'IA', 'StateCertificationIssued',
N'2019-03-26T22:12:29.4201277', 'Record Created', N'2019-03-13T00:00:00',
'DateCertificationIssued', N'2019-03-26T22:12:29.4201277',
'Record Created' ) ,
(N'CertificateIdentifier', '', 'IAC9985478', N'2019-03-26T22:12:47.3471693',
'U', '<CustomerCertificationID=327887>', NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
SELECT
ColumnName,
OldValue,
NewValue,
DateChanged,
ActionType,
AuditLogPK
FROM #AuditLogCustomerCert
WHERE ColumnName != 'Record Created'
UNION ALL
SELECT
CASE ColumnName
WHEN 'CT_OriginalValue' THEN 'Record Created - CertificationTypeID'
WHEN 'CI_OriginalValue' THEN 'Record Created - CertificateIdentifier'
WHEN 'SCI_OriginalValue' THEN 'Record Created - StateCertificationIssued'
WHEN 'DCI_OriginalValue' THEN 'Record Created - DateCertificationIssued'
END,
NULL,
NewValue,
DateChanged,
ActionType,
AuditLogPK
FROM (
SELECT
DateChanged,
ActionType,
AuditLogPK,
CAST(CT_OriginalValue AS NVARCHAR(MAX)) CT_OriginalValue,
CAST(CI_OriginalValue AS NVARCHAR(MAX)) CI_OriginalValue,
CAST(SCI_OriginalValue AS NVARCHAR(MAX)) SCI_OriginalValue,
CAST(DCI_OriginalValue AS NVARCHAR(MAX)) DCI_OriginalValue
FROM #AuditLogCustomerCert
WHERE ColumnName = 'Record Created'
) AS t
UNPIVOT (
NewValue
FOR ColumnName
IN (CT_OriginalValue, CI_OriginalValue, SCI_OriginalValue, DCI_OriginalValue)
) AS unp;
Output:
+-------------------------------------------+---------------------+---------------------------+---------------------+------------+----------------------------------+
| ColumnName | OldValue | NewValue | DateChanged | ActionType | AuditLogPK |
+-------------------------------------------+---------------------+---------------------------+---------------------+------------+----------------------------------+
| StateCertificationIssued | 66 | UA | 14/03/2019 13:04:45 | U | <CustomerCertificationID=327857> |
| createDate | Mar 14 2019 10:55AM | Mar 19 2019 1:52PM | 19/03/2019 13:52:29 | U | <CustomerCertificationID=327857> |
| createDate | Mar 19 2019 1:52PM | Mar 22 2019 10:43AM | 22/03/2019 10:43:42 | U | <CustomerCertificationID=327857> |
| createDate | Mar 22 2019 10:43AM | Mar 22 2019 2:38PM | 22/03/2019 14:38:57 | U | <CustomerCertificationID=327857> |
| StateCertificationIssued | UA | NJ | 26/03/2019 11:25:28 | U | <CustomerCertificationID=327857> |
| CountryCertificationIssued | Gabon | United States | 26/03/2019 11:25:28 | U | <CustomerCertificationID=327857> |
| createDate | Mar 22 2019 2:38PM | Mar 26 2019 2:23PM | 26/03/2019 14:23:27 | U | <CustomerCertificationID=327857> |
| CertificateIdentifier | IAC031900001 | IAC031900002 | 26/03/2019 22:02:21 | U | <CustomerCertificationID=327877> |
| CertificateIdentifier | | IAC9985478 | 26/03/2019 22:12:47 | U | <CustomerCertificationID=327887> |
| Record Created - CertificationTypeID | | Hunter Safety Certificate | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - CertificateIdentifier | | IA299038794 | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - StateCertificationIssued | | 66 | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - DateCertificationIssued | | Feb 28 2019 12:00AM | 14/03/2019 10:55:06 | I | <CustomerCertificationID=327857> |
| Record Created - CertificationTypeID | | Boater Safety Certificate | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - CertificateIdentifier | | IAC031900001 | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - StateCertificationIssued | | IA | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - DateCertificationIssued | | Mar 7 2019 12:00AM | 26/03/2019 10:30:19 | I | <CustomerCertificationID=327877> |
| Record Created - CertificationTypeID | | Temporarily Disabled | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
| Record Created - CertificateIdentifier | | | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
| Record Created - StateCertificationIssued | | IA | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
| Record Created - DateCertificationIssued | | Mar 13 2019 12:00AM | 26/03/2019 22:12:29 | I | <CustomerCertificationID=327887> |
+-------------------------------------------+---------------------+---------------------------+---------------------+------------+----------------------------------+
Test online with db<>fiddle.
Related
I found the next psql command:
CREATE TABLE #cdmDatabaseSchema.PERSON (
person_id integer NOT NULL,
gender_concept_id integer NOT NULL,
year_of_birth integer NOT NULL,
month_of_birth integer NULL,
day_of_birth integer NULL,
birth_datetime TIMESTAMP NULL,
race_concept_id integer NOT NULL,
ethnicity_concept_id integer NOT NULL,
location_id integer NULL,
provider_id integer NULL,
care_site_id integer NULL,
person_source_value varchar(50) NULL,
gender_source_value varchar(50) NULL,
gender_source_concept_id integer NULL,
race_source_value varchar(50) NULL,
race_source_concept_id integer NULL,
ethnicity_source_value varchar(50) NULL,
ethnicity_source_concept_id integer NULL );
What is the symbol # located just after CREATE TABLE for? Is there any special meaning/advantage to its use?
I am installing revive ad server on Ubuntu 20.4 which is having MySQL Version 8.0.23. The Log I got from file /var/debug.log is
[Last executed query: CREATE TABLE `bv_banners` (`bannerid` MEDIUMINT(9) AUTO_INCREMENT NOT NULL, `campaignid` MEDIUMINT(9) DEFAULT 0 NOT NULL, `contenttype` ENUM('gif','jpeg','png','html','swf','dcr','rpm','mov','txt') DEFAULT 'gif' NOT NULL, `pluginversion` MEDIUMINT(9) DEFAULT 0 NOT NULL, `storagetype` ENUM('sql','web','url','html','network','txt') DEFAULT 'sql' NOT NULL, `filename` VARCHAR(255) DEFAULT '' NOT NULL, `imageurl` VARCHAR(255) DEFAULT '' NOT NULL, `htmltemplate` MEDIUMTEXT NOT NULL, `htmlcache` MEDIUMTEXT NOT NULL, `width` SMALLINT(6) DEFAULT 0 NOT NULL, `height` SMALLINT(6) DEFAULT 0 NOT NULL, `weight` TINYINT(4) DEFAULT 1 NOT NULL, `seq` TINYINT(4) DEFAULT 0 NOT NULL, `target` VARCHAR(16) DEFAULT '' NOT NULL, `url` TEXT NOT NULL, `alt` VARCHAR(255) DEFAULT '' NOT NULL, `statustext` VARCHAR(255) DEFAULT '' NOT NULL, `bannertext` TEXT NOT NULL, `description` VARCHAR(255) DEFAULT '' NOT NULL, `adserver` VARCHAR(255) DEFAULT '' NOT NULL, `block` INT(11) DEFAULT 0 NOT NULL, `capping` INT(11) DEFAULT 0 NOT NULL, `session_capping` INT(11) DEFAULT 0 NOT NULL, `compiledlimitation` TEXT NOT NULL, `acl_plugins` TEXT DEFAULT NULL, `append` TEXT NOT NULL, `bannertype` TINYINT(4) DEFAULT 0 NOT NULL, `alt_filename` VARCHAR(255) DEFAULT '' NOT NULL, `alt_imageurl` VARCHAR(255) DEFAULT '' NOT NULL, `alt_contenttype` ENUM('gif','jpeg','png') DEFAULT 'gif' NOT NULL, `comments` TEXT DEFAULT NULL, `updated` DATETIME NOT NULL, `acls_updated` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL, `keyword` VARCHAR(255) DEFAULT '' NOT NULL, `transparent` TINYINT(1) DEFAULT 0 NOT NULL, `parameters` TEXT DEFAULT NULL, `status` INT(11) DEFAULT 0 NOT NULL, `ext_bannertype` VARCHAR(255) DEFAULT NULL, `prepend` TEXT NOT NULL, `iframe_friendly` TINYINT(1) DEFAULT 1 NOT NULL, PRIMARY KEY (bannerid)) ENGINE = INNODB]
[Native message: Invalid default value for 'acls_updated']
I got to know adserver is not officially supporting mysql version 8. But some people are able to run fine. I want to know how.
Here is the refrence I got
https://github.com/revive-adserver/revive-adserver/issues/1048
I want to run this.
Thanks in advance.
set sql_mode='40'' to sql_mode=''
in 5 files;
lib/OA/DB.php
lib/OA/DaL/Delivery/mysql.php
lib/OA/Dal/Delivery/mysqli.php
lib/OA/Upgrade/DB_Upgrade.php
lib/OA/Upgrade/Upgrade.php
and use table type INNODB
This trick works for me. I am calling this as a trick because it is not officially supported.
I got solution from github.
Here is the refrence
https://github.com/revive-adserver/revive-adserver/issues/1048
CREATE TABLE "dbo"."NPIData_HCP_PracticeAddress"(
[NPI] [varchar](55) NULL,
[ProviderPracticeAddress1] [varchar](255) NULL,
[ProviderPracticeAddress2] [varchar](255) NULL,
[ProviderPracticeCity] [varchar](55) NULL,
[ProviderPracticeState] [varchar](55) NULL,
[ProviderPracticePostalCode] [varchar](55) NULL,
[ProviderPracticePhone] [varchar](55) NULL,
[ProviderPracticeFax] [varchar](55) NULL
)
Try your CREATE TABLE statement like below by removing those square brackets []. In Oracle you use double quotes "" for escaping. See http://sqlfiddle.com/#!4/44c7d
CREATE TABLE "NPIData_HCP_PracticeAddress"(
NPI varchar(55) NULL,
ProviderPracticeAddress1 varchar(255) NULL,
ProviderPracticeAddress2 varchar(255) NULL,
ProviderPracticeCity varchar(55) NULL,
ProviderPracticeState varchar(55) NULL,
ProviderPracticePostalCode varchar(55) NULL,
ProviderPracticePhone varchar(55) NULL,
ProviderPracticeFax varchar(55) NULL
)
When I try to create an admin_user table on the server I get this error:
Base table or view not found: 1146 Table
The DDL:
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(32) NOT NULL DEFAULT '',
`lastname` varchar(32) NOT NULL DEFAULT '',
`email` varchar(128) NOT NULL DEFAULT '',
`username` varchar(40) NOT NULL DEFAULT '',
`password` varchar(40) NOT NULL DEFAULT '',
`created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified` datetime DEFAULT NULL,
`logdate` datetime DEFAULT NULL,
`lognum` smallint(5) unsigned NOT NULL DEFAULT '0',
`reload_acl_flag` tinyint(1) NOT NULL DEFAULT '0',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`extra` text,
PRIMARY KEY (`user_id`),
UNIQUE KEY `UNQ_ADMIN_USER_USERNAME` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
What is wrong?
SELECT M.msg_id, M.uid_fk, M.message, M.created,
U.fname, U.lname, M.uploads
FROM messages M, users_friends F, users U
WHERE M.uid_fk=F.friendID
and F.userID = '5'
and status='2'
Building a facebook-like wall and want to grab messages(updates) from friends.
The query above returns an empty set, even though I've made sure there are messages from user 5 in the table.
Schema:
CREATE TABLE IF NOT EXISTS `messages` (
`msg_id` int(11) NOT NULL AUTO_INCREMENT,
`message` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`uid_fk` int(11) DEFAULT NULL,
`ip` varchar(30) DEFAULT NULL,
`created` int(11) DEFAULT '1269249260',
`uploads` varchar(30) DEFAULT NULL,
PRIMARY KEY (`msg_id`),
KEY `uid_fk` (`uid_fk`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=263 ;
CREATE TABLE IF NOT EXISTS `users` (
`fname` varchar(15) NOT NULL,
`lname` varchar(15) NOT NULL,
`userID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(23) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`DOB` date DEFAULT NULL,
`sex` varchar(1) DEFAULT NULL,
`about` text NOT NULL,
`location` varchar(20) DEFAULT NULL,
`last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_level` int(11) NOT NULL DEFAULT '0',
`profile_image` varchar(200) NOT NULL,
`profile_image_small` varchar(200) NOT NULL,
PRIMARY KEY (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
CREATE TABLE IF NOT EXISTS `users_friends` (
`userID` int(11) NOT NULL,
`friendID` int(11) NOT NULL,
`status` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`userID`,`friendID`),
KEY `fk_users_has_friends_users1` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You are single quoting your INT values in the WHERE clause → '5' & '2'.
Also, try JOINs.
SELECT M.msg_id,
M.uid_fk,
M.message,
M.created,
U.fname,
U.lname,
M.uploads
FROM messages M
INNER JOIN users_friends F ON F.friendID = M.uid_fk
AND F.userID = 5
AND F.status = 2
INNER JOIN users U ON U.userID = F.friendID;