我需要一种按两列对模型进行排序的方法(但不是按x,y排序)。我有一个可以在结果中‘提升’模型的priority列,还有一个标准的created_at,当没有设置priority时,它应该是排序键。我找到的唯一方法就是与工会合作。
使用类似如下的查询:
(SELECT * FROM `charts` WHERE priority IS NOT NULL ORDER BY priority ASC)
UNION (SELECT * FROM `charts` WHERE priority IS NULL AND created_at IS NOT NULL ORDER BY CREATED_AT
我需要使用雄辩的执行以下查询,但不需要使用 DB::table函数。
SELECT a.name, a.type
FROM (
(
SELECT name, 'Customer' as `type`
FROM customers
)
UNION ALL
(
SELECT name, 'Supplier' as `type`
FROM suppliers
)) AS a
ORDER BY a.type ASC
LIMIT 20
到目前为止,我能够对这两个结果进行merge,但我不能使用别名a进行排序,也不能对结果进行limi
这个MySQL查询有联合和规范化,我不知道如何写最小的查询。 SELECT DISTINCT val
FROM
(
SELECT 'date' date_type, date val FROM user_presentation
UNION
SELECT 'accept_date', accept_date FROM user_presentation
UNION
SELECT 'question_date', question_date FROM user_presentation
UNION
我正在尝试运行以下“结果()”查询,并在我的视图中获取与foreach循环相关的以下错误:
public function index()
{
//show view of homepage
return View::make('index');
}
public function results()
{
$query = DB::table('Unions'); // Get the table before applying the where clauses
我有4个表,3个数据和2个数据透视表:
projects
-----------
id | name
tasks
------------
id | name | project_id
users
------------
id | name
project_user
------------
project_id | user_id
task_user
------------
task_id | user_id
现在我想要得到的是所有项目的列表,这些项目要么直接分配给特定用户,要么分配给该用户一个任务。
我已经尝试了很多东西,但不知何故,它不会真正工作,我
当我使用$query2->union($query1)时,这是以laravel表示的结果sql:
query one ...
union
query two ...
我怎么能有这样的查询:
query one ...
union distinct
query two ...
在中,我只看到了union()和unionAll()方法。
有可能在Laravel 5中执行此查询吗?
select t1.*, v.hash from
( select dr.*, d.id as d_id, d.name, d.user_id from directory d
left join directory_relations dr on d.id = dr.directory_id
union select dr.*, d.id as d_id, d.name, d.user_id from directory_relations dr
left join directory d on d.id =
我如何查询这个问题:
SELECT *, due1 as newDate FROM equipments WHERE due1 <> "1990-01-01"
UNION ALL
SELECT *, due2 as newDate FROM equipments where due2 <> "1990-01-01"
UNION ALL
SELECT *, due3 as newDate FROM equipments WHERE due3 <> "1990-01-01"
ORDER BY newDate
我可以使用以下代码一次在一个表中搜索任何列
<?php
namespace App;
use App\Services\Markdowner;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Post extends Model
{
protected $dates = ['published_at'];
protected $fillable = [
'title', 'subtitle', 'content_raw',
我有以下有说服力的问题:
$x = DB::table("x")->select("x.*");
$y = DB::table("x")->select("x.*");
$union = $x->union($y);
但是,如果我这样做了
$union->count();
它不能这样说:
Cardinality violation: 1222 The used SELECT statements have a different number of columns
请注意,我在联合中使用了两次
我试图找出最好的方法来定义下面的关系使用拉拉维尔的雄辩的拉拉维尔。
我有一个用户表和3个对象(球员,团队,联盟),用户可以添加为收藏夹。
我知道我可以用用户模型和每个对象创建三个枢轴表,但是接下来我需要运行一个Union查询来列出用户的所有收藏夹,不管类型如何。
User
id
Favorite
id
user_id
favorited_id ( player_id or team_id or league id)
favorite_type ( player , team or league)
Player
id
Team
id
League
id
这是我的
我有以下查询,它使用UNION和priority别名列:
SELECT id, name, 4 as priority FROM `topics`
WHERE id IN(17, 18, 19, 20, 21) AND name LIKE 'ge'
UNION
SELECT id, name, 3 as priority FROM `topics`
WHERE id IN(17, 18, 19, 20, 21) AND name LIKE '%ge%'
UNION
SELECT id, name, 2 as priority FROM `topics`
我不熟悉Laravel,但我想创建这个查询(MySQL):
SELECT
*
FROM
(
SELECT
ce.empresa_id,
CONCAT(ce.nombre, ' ', ce.apellido) AS nombre,
ce.grupo_contable
FROM
cliente_empresa AS ce
UNION
SELECT
cc.empresa_
下面是我的数据库结构的示例:
id | name | role | login_status |
-----------------------------------------------
1 mark user 1
2 john lawyer 0
3 david user 0
4 erik lawyer 0
我想要的是以这样一种方式对它们进行排序:具有lawyer角色的用户将位于列表的顶部
我需要使用以下查询从表中获取数据:
(select columns
from tableX
join tableZ
on tableX.id1 = tableZ.other_id)
union all
(select columns
from tableX
join tableZ
on tableX.id2 = tableZ.other_id)
LIMIT num1, num2
如果没有LIMIT,我将使用如下所示的查询生成器获得正确的结果(让$first是select的第一个查询,而$second是另一个select查询):
$first-
我想用分页的口才来表达MySQL的陈述
代码:
$query = "(SELECT city_name FROM Cities WHERE city_name LIKE '%" .
$keyword . "%')
UNION
(SELECT area_name FROM Areas WHERE area_name LIKE '%" .
$keyword . "%')
UNION
(SELECT sub_location_name FROM Sub_locations WHERE
我使用Laravel已经有一段时间了,我真的很喜欢它,但是我似乎已经无法使用查询生成器了。
我的查询中有多个联合,它们都是我想要的工作方式,但是当我添加->take()方法来限制我的结果时,会发生一些奇怪的事情,而不是我的查询末尾的限制,它位于第一个选择中。
现有的例子:
(select * FROM fruit LIMIT 1) union (select * FROM veg)
我希望它能发挥作用的例子:
(select * FROM fruit) union (select * FROM veg) LIMIT 1
基本代码示例:
$fruit = DB::table('fr
我在从两个表中获取某个字段的总和时遇到问题。
因此,作为开始,我的问题如下。
//Getting of Cost of Goods Sold (Menus)
$totalMenuCost = DB::raw('(SUM(orders.qty * orders.cost)) as cost');
$yearMenuSold = DB::raw('YEAR(orders.created_at) as year');
$menuscost = DB::table('orders')