我正在尝试运行的查询出现问题。查询得到3个单独的选择查询(每个查询都是自己工作的),但我仍然坚持将它们连接成一个结果。 2个选项包含相同的字段,但第3个包含不同的字段,我想知道是否有任何我可以做的事情而不是将查询分成2个单独的字段。在那一刻,它给了我1241错误。
当我将第一个和第三个选择语句结合在一起时,它工作正常,但显然中间查询具有不同的字段名称和数据。只是不确定你是否可以将sql数据提取到一个数组数组中,本来就更容易了!
欢呼,安德鲁SELECT(
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email`, `cas_users`.`meta`, `cas_users`.`id`
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '0'
AND `cas_users`.`expiry` > '1331165519'
) AS `c_p`, (
SELECT `name`, `email`, `phone`, `invited`
FROM `cas_not_connected`
WHERE `employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
) AS `nc`, (
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email`, `cas_users`.`meta`, `cas_users`.`id`
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '1'
AND `cas_users`.`expiry` < '1331165519'
) AS `c_e`
编辑:
这是我最终使用的代码,利用了完整的外连接解决方法。也许是矫枉过正,但确保只有1次数据库访问才能获得我需要的所有数据。 left_outer_join和right_outer_join可以是任何东西,只要它们保证不匹配。:
SELECT * FROM (
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email` AS `email1`, `cas_users`.`meta`, `cas_users`.`id` AS `id1`, `cas_connected`.`stage`
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '0'
AND `cas_users`.`expiry` > '1331173687'
UNION
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email` AS `email1`, `cas_users`.`meta`, `cas_users`.`id` AS `id1`, `cas_connected`.`stage` FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '1'
AND `cas_users`.`expiry` < '1331173687' )
AS `conn`
LEFT OUTER JOIN `cas_not_connected`
ON `conn`.`id1` = `cas_not_connected`.`name`
UNION
SELECT * FROM (
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email` AS `email1`, `cas_users`.`meta`, `cas_users`.`id` AS `id1`, `cas_connected`.`stage`
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '0'
AND `cas_users`.`expiry` > '1331173687'
UNION
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email` AS `email1`, `cas_users`.`meta`, `cas_users`.`id` AS `id1`, `cas_connected`.`stage` FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '1'
AND `cas_users`.`expiry` < '1331173687' )
AS `conn`
RIGHT OUTER JOIN `cas_not_connected`
ON `conn`.`id1` = `cas_not_connected`.`name`
答案 0 :(得分:0)
由于第二个查询中有不同的字段,因此UNION不会这样做。你必须将它们放在其他列旁边。我记住这条规则...... UNION将数据置于其他数据之上。 JOIN将数据放在其他数据旁边。
所以我认为你想要最终得到的是来自第一个和最后一个查询的列,中间查询的列被添加到这两个结果集的末尾。
所以我将第一个和最后一个查询合并在一起,然后通过employer_hash将结果集加入第二个查询。我正在使用UNION ALL,因此记录计数有意义(即query1 + query2 =新查询),尽管这可能不是您想要的。
同时执行左外连接,因此第二个查询不会指示哪些记录显示在结果集中。这可能需要一些微调,但应该让你到达你想去的地方。
SELECT * FROM (
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email`, `cas_users`.`meta`, `cas_users`.`id`, employer_hash
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '0'
AND `cas_users`.`expiry` > '1331165519'
UNION ALL
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email`, `cas_users`.`meta`, `cas_users`.`id`
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '1'
AND `cas_users`.`expiry` < '1331165519'
) AS `c_p_c_e`
LEFT OUTER JOIN (
SELECT `name`, `email`, `phone`, `invited`, employer_hash
FROM `cas_not_connected`
WHERE `employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
) AS `nc`, on c_p.employer_hash = nc.employer_hash
答案 1 :(得分:-1)
您可能想要使用UNION关键字
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email`, `cas_users`.`meta`, `cas_users`.`id`
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '0'
AND `cas_users`.`expiry` > '1331165519'
UNION
SELECT `name`, `email`, `phone`, `invited`
FROM `cas_not_connected`
WHERE `employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
UNION
SELECT CONCAT_WS(' ', `cas_users`.`first_name`, `cas_users`.`last_name`) AS `name`, `cas_users`.`email`, `cas_users`.`meta`, `cas_users`.`id`
FROM `cas_users`
INNER JOIN `cas_connected`
ON `cas_connected`.`freelancer_hash` = `cas_users`.`hash`
WHERE `cas_connected`.`employer_hash` = 'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
AND `cas_connected`.`stage` = '1'
AND `cas_users`.`expiry` < '1331165519'
这假定列都是相同的类型。看起来第二列包含一些不同的字段。