SQL id和no相同的有多条记录时,只取其中的一条,要怎么写查询语句

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL id和no相同的有多条记录时,只取其中的一条,要怎么写查询语句相关的知识,希望对你有一定的参考价值。

declare @A table (
id nvarchar(8),
[no] nvarchar(8),
a_string1 nvarchar(8),
a_string2 nvarchar(8)
);
insert into @A values('0001','0001','','');
insert into @A values('0001','0001','1111','1111');
insert into @A values('0001','0002','0000','0000');
insert into @A values('0001','0002','1111','1111');
insert into @A values('0001','0003','1111','1111');
insert into @A values('0001','0003','','');
insert into @A values('0001','0004','','');
insert into @A values('0001','0004','1111','1111');

参考技术A 如果查询只取id和no,可以用语句:
select distinct id , no from table追问

是查询四个字段

Laravel,如何在 HTML 表格中输出相同 ID 的多条记录?

【中文标题】Laravel,如何在 HTML 表格中输出相同 ID 的多条记录?【英文标题】:Laravel, how can I output multiple records of the same ID in a HMTL table? 【发布时间】:2021-08-20 17:00:33 【问题描述】:

我有一个包含许多产品的 orderitems 表。当我将 orderItems 输出到表时,它只显示一次。例如,如果我点了一份奶酪披萨,它只会在表格中显示其中一个,即使在 PHPMyAdmin 中有同一个披萨的多条记录。

所需的输出 在表格中显示多个具有相同产品 ID 的产品,而不是一个。

控制器

 <?php

namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use Illuminate\Http\Request;

class OrderItemsController extends Controller


    public function index()
    

        $orderItems = OrderItem::with('products_rel')->get();


    return view('basket', ['orderItems'=> $orderItems]);
    

    public function  create()
    
        return view('welcome');
    

    public function store ()
    
    request()->validate([
        'product_id'=> 'required',

    ]);



    OrderItem::create([

        'product_id' =>request('product_id'),



    ]);


    

刀片文件

<table id="checkout">
<tr>
<th>Product</th>
<th>Size</th>

<th>Price</th>
</tr>
@foreach($orderItems as $orderItem)
    @foreach($orderItem->products_rel as $related_product)
        <tr>
        <td>$related_product->name</td>
        <td>$related_product->size</td>

        <td>$related_product->price</td>
    </tr>


    @endforeach
@endforeach

产品表

 */
public function up()

    Schema::create('products', function (Blueprint $table) 
        $table->id();
        $table->timestamps();
        $table->foreignId('order_item_id');

        $table->string('name');
        $table->string('size');
        $table->float('price');
        $table->foreignId('topping_id')->nullable();



    );

OrderItems 表

 public function up()

    Schema::create('order_items', function (Blueprint $table) 

        $table->id();
        $table->foreignId('product_id');
       
        $table->timestamps();
    );

orderItem 模型

    <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Product;

class OrderItem extends Model


    use HasFactory;


    public function products_rel()
      return $this-> hasMany(Product::class );

    
    public function Order()
    return $this->hasMany(Order::Class);


产品型号

class Product extends Model


    use HasFactory;
    protected $fillable = [
        'item_name', 'size', 'toppings','price',
    ];

    public function orderItem() 
        return $this->belongsTo(OrderItem::class,);

    




DD 当我 dd $orderItems 出现所有订单项时:

Illuminate\Database\Eloquent\Collection #1069 ▼
  #items: array:5 [▼
    0 => App\Models\OrderItem #1213 ▼
      #connection: "mysql"
      #table: "order_items"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [▼
        "id" => 1
        "product_id" => 1
        "created_at" => "2021-05-27 16:50:48"
        "updated_at" => "2021-05-27 16:50:48"
      ]
      #original: array:4 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    
    1 => App\Models\OrderItem #1214 ▼
      #connection: "mysql"
      #table: "order_items"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [▼
        "id" => 2
        "product_id" => 1
        "created_at" => "2021-05-28 14:58:39"
        "updated_at" => "2021-05-28 14:58:39"
      ]
      #original: array:4 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    
    2 => App\Models\OrderItem #1215 ▼
      #connection: "mysql"
      #table: "order_items"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [▼
        "id" => 3
        "product_id" => 2
        "created_at" => "2021-05-28 15:23:34"
        "updated_at" => "2021-05-28 15:23:34"
      ]
      #original: array:4 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    
    3 => App\Models\OrderItem #1216 ▼
      #connection: "mysql"
      #table: "order_items"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [▼
        "id" => 4
        "product_id" => 2
        "created_at" => "2021-05-28 15:49:33"
        "updated_at" => "2021-05-28 15:49:33"
      ]
      #original: array:4 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    
    4 => App\Models\OrderItem #1217 ▼
      #connection: "mysql"
      #table: "order_items"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:4 [▼
        "id" => 5
        "product_id" => 3
        "created_at" => "2021-05-28 15:49:37"
        "updated_at" => "2021-05-28 15:49:37"
      ]
      #original: array:4 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
      #guarded: array:1 [▶]
    
  ]

【问题讨论】:

能否添加orderItem模型 @omaresmaeel 已更新 您在dd($orderItems); 上得到多个结果吗? @Espresso 是的,所有项目都出现在那里 @SVJ99 可以加一下吗 【参考方案1】:

我想我找到了问题

您通过在订单项表中添加产品的 id 来反转关系,而您应该做相反的事情

当你创建一个新的订单项时,这样做

$orderItemId = OrderItem::create([

    'product_id' =>request('product_id'),
])->id;

然后像这样将这个id添加到产品中

$productIds = ['1','2',''3] // hybothitecal product ids, and it should come from the request
$products = Product::find($productIds)->map(function($product)$product->order_item_id = $orderItemId;
$product->save(););

然后一切都会按预期工作*希望:'D *

【讨论】:

感谢您的回答,但不幸的是它没有工作 错误是什么?或者它只是没有工作 它只是做了和以前一样的结果,我需要做的就是将 orderItem 表中的所有内容输出到 html 表中。我不明白为什么它只输出 3 个结果 我认为因为现在它是空的,所以尝试向某些产品添加一个订单项目,现在,你正在为一个产品添加一个订单项目,所以它和以前一样,但是如果你发送一个产品数组和它们的 orderItem Id。它会正常工作,我已经更新了我的答案 我找到了原因。它正在输出产品表中的所有内容。我希望它输出 orderItems 表中的所有内容,所以也许我的关系是错误的?

以上是关于SQL id和no相同的有多条记录时,只取其中的一条,要怎么写查询语句的主要内容,如果未能解决你的问题,请参考以下文章

sql用inner join内关联查询有多条记录一样只取一条?

多表查询结果出现重复记录,根据条件只取其中的一条记录的sql语句

数据库sql,重复字段只取其中一行

sql如何取group by 分组的多条记录只取最上面的一条!

sql语句,合并多条记录中的相同字段。

将多条记录插入 SQL 表时,它不遵循顺序