last day (15 days later) » 

7:27 PM
2
A: Join database and count

Tobi KlostermairYou need the group by statemant: select users.name, count(*) as counter from users join cars on ... group by users.name; Okay here in more Detail: You have to join the users table with the cars table. You do that, yes. Then you have to select one col from the user table and one count(*) as...

 
Check out my edition in the question.
@AliGajani - I will try this. Thanks
@Tobi - $users = Users::select(DB::raw('count(*) as counter'))->join('cars', 'cars.owner', '=', 'user.id')->groupBy('users.id')->get(); - I tried that way and it's returning only the 'counter' and I need the other columns as well.
I fixed that way: $users = Users::select('*', DB::raw('count(*) as counter'))->join('cars', 'cars.owner', '=', 'user.id')->groupBy('users.id')->get();
 
yes :-) good work
 
I have another problem now... I need to know how many places sells those cars based on the id of the current car. I tried that way: $users = Users::select('*', DB::raw('count(*) as counter'))->join('cars', 'cars.owner', '=', 'user.id')->join('places', 'places.car', '=', 'cars.id')->groupBy('users.id')->get(); - And it's returning only 1 always.
And another problem I found now is that if the users doesn't have a car registered it doesn't return the user.
 
Yes you need for that a other join statement.
 
I don't understand. The Full Outer Join is to fix the problem of listing the user even if there's no car registered for him? Or the problem of to count the places that has the car based on the the id of the car?
 
7:27 PM
it was for that problem, that you dont get a user, when he hasnt a car...the other problem i havnt seen ... i dont know if i get that problem...you want to know, how many places are connected with one car?
 
Yes, for example. I want to list all users, and along with that I want to know how many cars that user has and how many places has the same car. And list like that: User X has N cars that sells in N places. - This mean that the places will be counted by the ids of the cars.
 
or has a car exact one "place" as a number?
ah okay one moment
in that way its impossible i think, because you want to know
 
Are you there?
 
ye
okay, so thats impossible
that re 2 different querys
1. how many cars have a user (here the primary factor is the user)
and
 
I understand what you say because each car has an id, right?
 
7:30 PM
2. how many places are connected with one car
 
And in the query there's no loop.
 
ye, but when you count(), then you have no car_id :-)
 
That is the problem, because I have a page to create an I need to list this together.
How I can make two queries and return as one? I mean, one single array?
 
hmm i think, i dont understand anything, mom, let me think about it
when you have 2 users
 
Let me explain and then you will get it.
 
7:33 PM
and you have 4 cars, 2 for each user
then you have:
user1 -> 2 cars
 
Yes, keep going.
 
user2 -> 2 cars
and one car have more selling places?
yes?
 
More or less.
I will explain better.
One second.
 
ok
my english is a hell :-)
 
Let's imagine we have two users. user1 and user2, each one can have several cars but they aren't that rich to afford that (haha), then each one has 2 cars only, which is 4 cars in total, but we are selling only two models of cars, which is car1 and car2, then user1 has car1 and car 2, and the user2 has car1 and car2 as well, even though we have 2 cars only we have 4 of all clients. A car has several places that you can buy and each place has a car registered to sell, like:
place1 sells car1 only and place2 sells car1 and car2.
Then on the listing
User1 has 2 cars that sells in 2 places (because the car1 sells on place1 and place2)
And...
User2 has 2 cars that sells in 2 places (because the car1 sells on place1 and place2)
 
7:38 PM
ah okay
okay
 
Now let's imagine that appear a client3 and he has only the car2, which means that sells only in 1 place.
if appears client4 and he has only car1 means that sells in 2 places.
 
yes i understand, i think about it, difficult
mom
 
Yeap, it's really hard. :(
Because of that I assumed this: $users = Users::select('', DB::raw('count() as counter'))->join('cars', 'cars.owner', '=', 'user.id')->join('places', 'places.car', '=', 'cars.id')->groupBy('users.id')->get();
But I forgot that each car has an id then the join of places won't work because it will take only the last or the first (idk) id of cars.
And as you said, car id doesn't exist, right?
I have so many things to do, and i'm stuck on this. :'(
 
after that count() not, but before, i dont know how it is exactly, but i think i know the workflow
1. select car_id, count() cars join places on ... group by car_id
2. select user.id, count(*) from user join cars on ... SUM(select1.count)
 
This will I will get how many places sells the car, right?
 
7:48 PM
1
Q: Select sum and inner join

KlausI have two tables Bills: id amount reference Transactions: id reference amount The following SQL query SELECT *, (SELECT SUM(amount) FROM transactions WHERE transactions.reference = bils.reference) AS paid FROM bills GROUP BY id HAVING paid<amount was meant to some rows f...

yes exact
 
I mean, the 1
 
and that subselect you have to SUM() in the other
 
The 2 is to sum the 1 with how many cars each user has.
 
yes the 1
:-)
 
Is that it?
 
7:49 PM
yes i think it is
 
What you mean with select1.count in the 2º
I'm using Laravel Eloquent and it has a method called count() that count the results and returns in a variable.
 
the 2 is to sum the first select, and to count the cars per user
 
And I also can put the raw query as count()
Is it two different queries or just one? I'm kinda lost.
 
2 different in one
:-)
but i have to say, i dont get it as a select
i try a little bit with that, its interesting
 
Sorry for my ignorance and laziness, but I still lost... Because o 2
ops...
o 2º I see select1.count
What is it?
And I can see two selects, how can this be one just query?
I think I got it.
 
7:54 PM
hey i think that helps too, stackoverflow.com/questions/27351274/…
 
It's like SELECT *, (SELECT ..)...
And I thought once I knew SQL. oh gosh. haha.
I will try to see if that works. okay?
I will try to use the raw query first instead of the methods of the framework, using the raw I can change faster.
Just one second.
But I still lost in something... how can I put the two selects of yours together? O.o
 
8:07 PM
I'm completely lost. I don't know how to put what you sent together.
 
select user.id, count(*) as count1 from users
join cars on ...
join (

select user.id as u_id, SUM(places) as sum from users
join cars on user.id = car.owner
join places on cars = places

) as second_select
on second_select.u_id = user.id
group by user.id
here :-)
it should be work
but no idea how to implement with that raw thins
i explain mom
the subselect in the mid do the following: get all users and sum(all places who are connected with a user over a car)
when you join that on the main select on user id, you have on all users the correct places
 
I will try.
 
then group by user id
and yes, thats it
its hard
 
I know, really complicated.
I need to deliver this and much more tomorrow morning. I'm really f**. D
 
:-) yeah ... when u ve a question u can ask me...bye bro
my first day on stackoverflow :-) funny
 
8:14 PM
hahaha.
How can I ask you after?
@Aminul - Are you there?
 
yeap
 
Did you read the chat?
I'm having a really headache with this.
How to make two selects in one single query as you can see in above that Tobi posted?
Using Eloquent.
 
yes i have read, may be i have n't understand every thing. ok, could you tell me do the cars have any relations with places?? if they then you could use cars.places in eager loading
 
The only relation that places has with cars is that it holds id of cars that the store sells.
For example, place1 sells car1 and car2.
And place2 sells only car1
Then if the user1 has 2 cars, each one of one type then the car sells in 2 places
The listing is like this:
user1 has N cars that sells in N places
 
8:30 PM
that's mean they have many to many relation, if you have any pivot table then you can do that
 
I have one Model to each one.
Like: CarModel, PlacesModel, and UsersModel
I need to set CarModel method count as belongtoMany PlacesModel? Is that it?
 
but these are many changes
actually no, you have set CarModel belongtoMany PlacesModel and PlacesModel belongtoMany CarModel and one table to store their ids
 
their id?
 
if you got all the places then you could count them
 
hm.. I think I'm getting you now.
 
8:37 PM
yes if you need to support many to many relationship
 
I need to create a method, for example, in PlacesModel called countPlaces() and inside that method call belongtoMany('CarModel').
And inside of that method countPlaces count the total of places.
But here is the question, how the methods is going to know the id of each car?
And then count the places that this id exists?
 
yeah thats the problem
 
do your CarModel have any function named places
where you put the relation: $this->belongsTo('PlaceModel')
 
It doesn't
CarModel only has the id and informations about the car
The PlaceModel has ids of cars
It's like this: Users has Cars and Places Has Cars. I need to know how many cars a user has and how many places sells the cars the user has.
...
 
9:02 PM
sorry i have tried, i need to know you db tables stucture
then may be i could do it in one query. but not sure
 
I will tell you
the table users has the following columns
id
ops..
user_id
user_name
and car has
car_id
car_name
car_owner
and the places
place_id
place_car
 
place car is car_id??
 
yes
and car_owner is the user_id
 
and you need place count where car is selled
 
and how many cars the user has
in a listing of users.
userY has N cars that sells in N places
user has N cars that sells in N places
ops..
userX has N cars that sells in N places
example.
 
9:11 PM
do have any pivot table??
 
I don't know what is pivot table :(
If you know what it is and if this is going to work you can create an tell me after.
please :(
 
if not then i think you should do this in multiple query
 
What is the best way in your opinion and if it's possible to do
 
i am happy to but it very late here.
and i need to sleep
you can follow this
 
The thing is that I don't have much time now and I need to get this one by tomorrow morning :(
I really need your help here
Look, the thing with multiple queries is that the places has different cars then I need to do a kind of loop
And so on and then append.
 
9:15 PM
then do it in multiple query, after finishing you can optimize it
 
I'm completely lost.
What you mean with multiple query and that works in this case?
 
the kind of loop
 
Any example?
Are you from chine or something like that?
To be late and so on.
Bangladesh
 
I m from Bangladesh
and it's 3:17am
yeap
where you from?
 
Almost indian, but asian.
I understand, it's really late for you
South America
 
9:19 PM
Not Indian, Bangladeshi :)
 
I know, I said almost because it's surrounded by India right?
But still, asian. :D
 
:D
we can sort it later
just finish your task
 
The problem is that I need to deliver this tomorrow morning, and for you it will be night.
This mean my head will be handing haha.
Ok, I will leave you for you to sleep.
Have a good night and thanks.
 
just return the data and see it any json parser
you will found a way
 
Yeap, I will try. :D
 
9:22 PM
loop through each object
 
get the id and make another query count and return )
I had this in mind, I will try.
 
ok, thats the spirit
ok then good night
 
good night :D
 

last day (15 days later) »