SELECT id, name, marks
FROM
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY id) rn,
COUNT(*) OVER (PARTITION BY name) cnt
FROM your_table
) t
WHERE rn = 1 AND cnt > 1;
现在我应该如何优化这个,因为唯一的id user_first_name和卡号是相同的
答案 0 :(得分:6)
根据您的if检查,不需要if检查,因为如果$value->name == ''
您要在$user->user_first_name = ''
中保存空字符串,那么您可以删除,如果检查
$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->user_first_name = $value->name;
$user->card_id = $value->cardnumber;
$user->save();
编辑更新的代码库,您可以将代码重构为
foreach ($remote_user_detail as $value){
$user = User::where('unique_id',$value->userid)->first();
if(count($user) == 0){
$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
}
$user->user_first_name = $value->name;
$user->card_id = $value->cardnumber;
$user->save();
}
答案 1 :(得分:2)
正如Khalid所指出的,你不需要检查,因为你已经分配了空值,所以这是一种浪费。此解决方案仅用于添加其他任何内容。
只需在$ user-> user_first_name上使用三元opertor,如
$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->user_first_name = ($value->name=='') ? '' : $value->name ;
$user->card_id = $value->cardnumber;
$user->save();
答案 2 :(得分:1)
可能是这样的吗?
$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->card_id = $value->cardnumber;
if($value->name=='') {
$user->user_first_name = '';
}else {
$user->user_first_name = $value->name;
}
$user->save();
答案 3 :(得分:1)
在if语句中,我只会根据条件放置正在改变的事物。在那里,您可以应用 DRY 原则。
请点击此链接了解更多详情http://web-techno.net/dry-principle-explained/
在你的情况下,我将选择三元运算符:
如果这是新手,请查看此https://davidwalsh.name/php-shorthand-if-else-ternary-operators
arr3
答案 4 :(得分:0)
我建议你创建一个User构造函数,只需将用户作为参数1传递,将$ value作为参数2传递
class User {
//definitions here
function __construct2($user,$value)
{
company_id = $user->company_id;
user_label = 2;
unique_id = $value->userid;
user_first_name = $value->name;
card_id = $value->cardnumber;
}
}
如果想要重新分配这样的东西,这会高度清理代码以供将来使用。
$user = new User(Auth::user(), $value);
另外,如图所示 - 不需要if语句
答案 5 :(得分:0)
我在2条件中看到的$user->user_first_name
总是等于$value->name
。
看看你自己if($value->name=='') {$user->user_first_name = ''}
所以只要不用其他就写代码。
$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->user_first_name = $value->name;
$user->card_id = $value->cardnumber;
$user->save();
此外,如果您有其他情况使用PHP 7 Null合并运算符,则该功能的一个建议是链接和示例
http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
答案 6 :(得分:0)
foreach ($remote_user_detail as $value){
$user = User::where('unique_id',$value->userid)->first();
if(count($user) > 0){
$user->unique_id = $value->userid;
$user->user_first_name = $value->name;
$user->card_id = $value->cardnumber;
$user->save();
}else{
$user = new User();
$user->company_id = Auth::user()->company_id;
$user->user_label = 2;
$user->unique_id = $value->userid;
$user->user_first_name = ($value->name=='') ? '' : $value->name ;
$user->card_id = $value->cardnumber;
$user->save();
}
}
现在我应该如何优化这个,因为唯一的id user_first_name和cardnumber是相同的