i need some help with my query, basically im trying to get all my order_items on the table, but i need to get all items by user_id, but there is a detail, i want to include also records that includes on a column more than 3 times the the same email on different users_id.
Ex:
table:
- id;
- user_id;
- email;
1- Get all records from mine (user_id); 2 - Get all records where on 3 differentes user_id have the same email value;
Here is my query:
$orders = OrderItem::select('email','user_id')->where('user_id',Auth::user()->id)->distinct('email')->get();
This should work:
$orders = OrderItem::select(DB::raw('count(*) as email_count,email,user_id'))
->groupBy('email')
->where('email_count','>',2)
->orWhere('user_id',Auth::user()->id)
->get();
Related
I am creating report to pull out Duplicate employee number from user table in Servicenow.
in a classless script include:
Use Glide Aggregate function
groupBy employee number
addaggregate count on employee number
count > 1
return sys_id all the records
Report:
put filter sys_id IN javascript:
var dpchk = new GlideAggregate('sys_user');
dpchk.groupBy('employee_number');
dpchk.addHaving('COUNT', '>', 1);
dpchk.query();
while(dpchk.next())
{
gs.print('Therefore the duplicate employees number is'+dpchk.employee_number);
}
I have two tables contact_us and upload_new_car.
contact_us table contains columns:
id
name
email
phone
message
created_at
updated_at
upload_new_car contains columns:
id
name
phone_number
car_name
car_price
location
car_model_year
car_model
variant
driven
fuel
transmission
city
no_of_owners
upload_1
upload_2
upload_3
upload_4
upload_5
created_at
updated_at
How can I get the UNION of these tables in Laravel? Please help
Not allowing to different size of columns is not laravel's businuess. It is a matter of SQL .You can follow [this link] for more info about UNION statements .
On the other hand for laravel you can use those syntax using union (We can benefit from selecting the same count of columns from each tables).
$first = DB::table('contact_us')
->select('name','phone');
$users = DB::table('users')
->select('name','phone_number as phone')
->union($first)
->get();
dd($users)
I have the following prices-table:
shop_id (int)
product_id (int)
price (float)
created (DateTime)
Every hour a cronjob checks the shops and inserts new entries (current prices) into these price-table.
Now I want to display the newest price for a product. I have to GROUP BY the shop_id because I only want one price per shop but I only want the newest entry (created).
Can I solve this with Eloquent Query-Builder or do I have to use raw SQL? Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
You can try it as:
Price::select('*', DB::raw('MAX(created_at) as max_created_at'))
->groupBy('shop_id')
->get()
Assuming model name is Price
Eloquent (purist) approach:
Price::orderBy('created', 'desc')->groupBy('shop_id')
->get('shop_id', 'price');
References:
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_orderBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_groupBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_get
*untested though
Q: Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
A: you could pass it to Model's contructor - but it might need model's field to be fillable - or hydrate a model. Alternatively, just access it like an keyed-array, ie. $something[0]['price'] <-- assuming an array of prices with price column.
I solved the problem without QueryBuilder. Instead I use a raw SQL-statement and generating the models with the hydrateRaw()-function of the Model-class.
$prices = Price::hydrateRaw( 'SELECT p.*
FROM prices p
INNER JOIN (
SELECT shop_id, max(created_at) AS max_ca
FROM prices p1
GROUP BY shop_id
) m ON p.shop_id = m.shop_id AND p.created_at = m.max_ca');
I have a Post and for each post there are multiple Likes in the likes table. I have created a total_likes field in the Posts table and I want to update it with the total number of likes from the likes table, in one statement. I current have this:
DB::table('posts as p')
->update(['total_likes' => function($query) {
$query->select(DB::raw('COUNT(*)'))
->from('likes as l')
->where(DB::raw('p.id = l.post_id'))
->groupBy('l.post_id');
}]);
But I am getting an error:
Object of class Closure could not be converted to string
I'm essentially trying to run:
UPDATE posts SET total_likes = (SELECT COUNT(id) FROM likes WHERE likes.post_id = posts.id GROUP BY id);
I am unable to select the rows where TestId is max for respective student, I wrote the code as follows which does not get the required output. my code is as follows,
Criteria c = sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList().add(Projections.property("answer"),"answer"));
c.add(Restrictions.eq("surveyId",send_Survey));
//c.add(Restrictions.eq("testId", "1" ));
//c.setProjection(Projection.max("testId"));
c.addOrder(Order.desc("testId"));
c.add(Restrictions.eq("questionid",FinalQuestionsOne));
List<String> age=c.list();
My table structure is as follows,
I need the following output. select the answer column for max TestId's. How can I get the output using criteria query
So I think what you're trying to get can be achievedd by the following sql:
SELECT TestId, MAX(answer) WHERE questionId = 1 GROUP BY TestId;
You should be able to achieve this with the following Hibernate:
sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList()
.add(Projections.property("TestId"), "TestId")
.add(Projections.groupProperty("TestId"))
.add(Projections.max("answer")));