查询表单2列,但从另一个相应列中提取数据



编辑:

以下是我能够进行的查询。。。一些什么。为了澄清,表中有多列,但需要查询其中的2列,但也需要作为单列。而且我真的想要我想要查询的2的相反列。如果列a=用户#,我希望数据在列b中,反之亦然。

下面是我所拥有的,它确实有效,但我有两个错误。

mysqli::stat():无法在/home/中获取mysqli。。。。。。。注意:unserialize():偏移处的错误

这是代码;


$db=$this->getDbo();$query=$db->getQuery(true);

// Select the required fields from the table.
$query->select('d.*, uida AS contact ');
$query->from('`#__user_memberlinks` AS d');
$query->where('d.uidb =' . $userID);
$query->where('d.usera = 1')->andWHERE ('d.userb = 1');

// Join over the users for the checked out user.
$query->select('uc.name AS uEditor');
$query->join('LEFT', '#__users AS uc ON uc.id=d.checked_out');
// Join over the created by field 'created_by'
$query->join('LEFT', '#__users AS created_by ON created_by.id = d.created_by');
// Join over the created by field 'modified_by'
$query->join('LEFT', '#__users AS modified_by ON modified_by.id = d.modified_by');



$db    = $this->getDbo();
$query2 = $db->getQuery(true);
// Select the required fields from the table.
$query2->select('b.*, uidb AS contact');
$query2->from('`#__user_memberlinks` AS b');
$query2->where('b.uida =' . $userID);
$query2->where('b.usera = 1')->andWHERE ('b.userb = 1');

// Join over the users for the checked out user.
$query2->select('uc.name AS uEditor');
$query2->join('LEFT', '#__users AS uc ON uc.id= b.checked_out');
// Join over the created by field 'created_by'
$query2->join('LEFT', '#__users AS created_by ON created_by.id = b.created_by');
// Join over the created by field 'modified_by'
$query2->join('LEFT', '#__users AS modified_by ON modified_by.id = b.modified_by');

$query->union($query2);

谢谢

组织。为历史留下的帖子

我在一个表中有两列;

<1><1><1><1>
A列B列UA
1112221
222333
222444
1113331
555222

您的基本方法应该是使数据变平,这样您就可以像对待普通查询一样对待它。您没有指定RDBMS,但这适用于SQL Server

--Set up sample data
;with cteTabA as ( 
SELECT * FROM ( VALUES 
('111', '222', '1', '2')
, ('222', '333', '11', '21')
, ('222', '444', '12', '22')
, ('111', '333', '13', '33')
, ('555', '222', '14', '41')
) as TabA(ColA, ColB, UA, UB) 
) SELECT * 
INTO #Sample
FROM cteTabA
--SELECT * FROM #Sample --Display test data
--Set up what we're looking for
DECLARE @LookFor char(3) = '222';
--Solution - flatten the table, but log where the ID came from
with cteFlat as (
SELECT ColA as ID, UB as Opposite, 'A' as MatchCol
FROM #Sample
UNION ALL 
SELECT ColB as ID, UA as Opposite, 'B' as MatchCol
FROM #Sample
)--Now just search for it
SELECT * FROM cteFlat as F 
WHERE ID = @LookFor 
--Clean up 
DROP TABLE #Sample 

输出:

AB[/tr>B[/tr>
ID对面MatchCol
22221
222A
2221
22214

跟进;

上述《工会守则》确实有效。

未序列化的错误是由Joomla的Debugging函数引起的,如果调试关闭,则不会显示在视图中。不确定原因,或者是否是问题,因为它确实正确地检索了数据。如果我理解正确的话,Joomla有一种处理未序列化数据的方法。

";mysqli::stat():无法在/home/中获取mysqli"错误,通过将服务器、数据库类型更改为MySQL(PDO)而消失。猜测MySQLi不能很好地处理联合查询。错误似乎来自库/jommla/database/driver/mysqli中的文件。。。

谢谢大家的帮助!

最新更新