将3个查询组合成一个数组(combining 3 queries into one array)
有一个更好的方法吗? 我有3个不同的查询,并希望将它们全部放入一个数组中。 因为我不能直接按照我想从查询中直接得到的方式排列它们,所以我必须在3个单独的查询中完成它(除非您知道在查询中执行此操作的方法)。
然后,我不得不创建单独的临时数组,所以我可以做一个foreach循环来使用array_push()函数。 肯定有比这个笑声更好的方法。
$sql = "SELECT prayers.* FROM prayers LEFT JOIN prayed_for USING (postid) WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' and prayers.type = 'prayer' and prayers.privacy != 'hidden' and ( (priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 20 DAY)) or (priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 40 DAY)) ) ORDER BY priority DESC, prayers.date DESC"; $sql2 = "SELECT prayers.* FROM prayers LEFT JOIN prayed_for USING (postid) WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' and prayers.type = 'prayer' and prayers.privacy != 'hidden' and priority = 'normal' and date > DATE_SUB(NOW(), INTERVAL 60 DAY) ORDER BY prayers.date DESC"; $sql3 = "SELECT prayers.* FROM prayers LEFT JOIN prayed_for USING (postid) WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' and prayers.type = 'prayer' and prayers.privacy != 'hidden' and priority = 'long_term' ORDER BY prayers.date DESC"; $_SESSION['aprayers'] = send_query($sql); $rows2 = array(); $rows2 = send_query($sql2); foreach ($rows2 as $row) { array_push($_SESSION['aprayers'],$row); } $rows3 = array(); $rows3 = send_query($sql3); foreach ($rows3 as $row) { array_push($_SESSION['aprayers'],$row); }
Is there a better way to do this? I've got 3 different queries and want to put them all into one array. Since I can't just order them exactly the way I want straight from the query I'm having to do it in 3 separate queries (unless you know of a way to do it IN the query).
Then I'm having to create separate temporary arrays just so I can do a foreach loop to use the array_push() function. Surely there's a much better way than this lol.
$sql = "SELECT prayers.* FROM prayers LEFT JOIN prayed_for USING (postid) WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' and prayers.type = 'prayer' and prayers.privacy != 'hidden' and ( (priority = 'emergency' and date > DATE_SUB(NOW(), INTERVAL 20 DAY)) or (priority = 'urgent' and date > DATE_SUB(NOW(), INTERVAL 40 DAY)) ) ORDER BY priority DESC, prayers.date DESC"; $sql2 = "SELECT prayers.* FROM prayers LEFT JOIN prayed_for USING (postid) WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' and prayers.type = 'prayer' and prayers.privacy != 'hidden' and priority = 'normal' and date > DATE_SUB(NOW(), INTERVAL 60 DAY) ORDER BY prayers.date DESC"; $sql3 = "SELECT prayers.* FROM prayers LEFT JOIN prayed_for USING (postid) WHERE prayed_for.username = '" . $_SESSION['user']['username'] . "' and prayers.type = 'prayer' and prayers.privacy != 'hidden' and priority = 'long_term' ORDER BY prayers.date DESC"; $_SESSION['aprayers'] = send_query($sql); $rows2 = array(); $rows2 = send_query($sql2); foreach ($rows2 as $row) { array_push($_SESSION['aprayers'],$row); } $rows3 = array(); $rows3 = send_query($sql3); foreach ($rows3 as $row) { array_push($_SESSION['aprayers'],$row); }
原文:https://stackoverflow.com/questions/31572456
最满意答案
如果在运行时期间得到
NoClassDefFoundError
,那么我怀疑你没有包含.jar文件在运行时工作。 您必须选中每个.jar的复选框,如下所示:The solution to the problem was (as Selvin stated in his comment) to include the full jackson instead of the jackson mini library. Thanks again Selvin.
相关问答
更多-
是不是有一款什么手机软件,可以很自然的拉长腿部?不是美图秀秀,有知道的不?[2022-10-20]
spring -
春天的英语怎么读?[2022-05-21]
spring -
您不能拥有多个@RequestBody注释。 @RequestBody带注释的参数应该保存请求的整个主体并绑定到一个对象。 你应该使用不同的方法,如 引入封装您的字符串的包装器对象,并更改您的签名。 例如 包装对象 class MyWrapper{ String name, name2; //ToDo: Create constructors String getname(){ return name; } String getNam ...
-
您可能缺少杰克逊依赖项。 如果您使用的是maven,则可能需要添加它。
com.fasterxml.jackson.core jackson-databind ${jackson.databind-version} -
这个类是jackson-core-asl.jar一部分,它包含在类路径中。 This class is part of the jackson-core-asl.jar include it in the classpath.
-
Android Spring Jackson Mapping:NoClassDefFoundError(Android Spring Jackson Mapping: NoClassDefFoundError)[2022-07-29]
如果在运行时期间得到NoClassDefFoundError ,那么我怀疑你没有包含.jar文件在运行时工作。 您必须选中每个.jar的复选框,如下所示: The solution to the problem was (as Selvin stated in his comment) to include the full jackson instead of the jackson mini library. Thanks again Selvin. -
不确定你是否已经通过更新你的罐子解决了这个问题,但是这里有一些答案: 在深入挖掘后,我发现在jackson的github中的JsonPropertyDescription类没有与注释2.2.3 jar一起打包。 这是2.3中的新功能 。 有没有新的方法我没有看到拿一个对象并将其打印到json? 我不这么认为: writeValueAsString()2.3.3 (它不被弃用)。 看起来有人有类似的问题 ,他们建议的解决方案基本上是“更新你的罐子”。 提问者没有回复,所以我们不知道这是否有效。 Not sur ...
-
如果你使用Jackson 2,那么你需要使用Jackson2SpringAndroidSpiceService而不是JacksonSpringAndroidSpiceService 。 2件事:) If you use Jackson 2, then you need to use a Jackson2SpringAndroidSpiceService and not a JacksonSpringAndroidSpiceService. The 2 matters :)
-
根据这个线程 ,JSON规范本身并不禁止具有相同名称的多个字段(在您的情况下,在Daives对象中名为“Daive”的多个字段)。 但是,大多数解析器将返回错误或忽略除最后一个之外的任何值。 如你所说,将这些值放入数组似乎更合理; 事实上,你将能够将这个数组映射到杰克逊的List。 According to this thread, the JSON spec itself does not forbid multiple fields with the same name (in your case, m ...
-
重要的是要了解杰克逊首先反序列化请求正文的JSON内容并创建UserAgreement对象。 只有这样才能通过@Valid注释应用标准bean验证。 因此,只有在JSON反序列化成功后才能将@Valid视为一个额外的验证层。 使用@ExceptionHandler方法进行反序列化错误。 It's important to understand that first Jackson deserializes the JSON content of the request body and creates a ...