I'm trying to list informations about a table and one of that information is how much cars an user has. I have two databases, one is users and the other is cars. The table cars has a column that is owner that holds the id of the owner. What I want to know is: How to list all users and along with that the total of cars that each user has?
$users = Users::all();
This code returns an array with all users, what I want is to pass the total of cars that each user has on the same $users variable. How can I do that? Is there a way to join the other table, count and then return or something like that?
#edit
I tried like this, but doesn't work:
$users = Users::join('cars', 'cars.owner', '=', 'users.id')->select(DB::raw('count(cars.car_id) as total'))->get();
You 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 counter
The trick is now, to "group by" the col from the users table. That matches all double user rows to one row and count how much cars one user has
The select statement is:
"select users.id, count(*) as counter from Users join Cars on cars.owner=users.id group by users.id"
Thats all ... hope that help you
I think this will help you...
Users::with([
'cars' => function($q){
$q->select([DB::raw("count(car_id) as total"), "car_id"])
->groupBy('car_id');
}
])->get();
You can try this SQL query in raw. I tried and it works.
SELECT users.name as User_Name, COUNT(cars.user_id) as Car_Count
FROM users
LEFT JOIN cars
ON users.id=cars.user_id
GROUP BY users.id
Related
I have a table with conversation ids which is a one-to-many relationship to another table that has conversation users. The conversation users table has the id for the conversation, a unique id and a userID. I can use this:
SELECT c_id FROM conversations INNER JOIN conversation_users on conversation_users.cu_convo_id = conversations.c_id
which gives me all the rows but I need to find a way to do something like an IN statement because I need to find the conversation id given two user IDs.
Any ideas? Am I approaching this the wrong way completely?
WORKING ANSWER:
IN case anyone needs it, the following worked for me:
SELECT cu_convo_id FROM conversations
INNER JOIN conversation_users on
conversation_users.cu_convo_id = conversations.c_id
WHERE cu_user_id IN (31,42)
GROUP BY cu_convo_id
HAVING count(distinct cu_user_id) = 2;
if I have 5 IDs to find, i put them on line 4 and then put the number 5 on line 6
Simply add a WHERE to your SQL:
SELECT c_id FROM conversations
INNER JOIN conversation_users on
conversation_users.cu_convo_id = conversations.c_id
WHERE conversations.usedID IN (1,2);
Okay, so I have two tables, a news table and a users table. They are set up as below:
news:
id title user_id contents
users:
id username password
When I run this query:
SELECT news.title, users.username, news.contents FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
I can only access the user id using $query['id']. So it appears that the join is using the column names from table2, although I want them to map it to the column names of table1. So even though the column is called id in the users table, I want to access it under user_id, since I specified that in the JOIN query.
I need this because if I ever need the id from table1, they would both be mapped to the column called id and I would only be able to get the id of the users table.
So is there any way to do this? Access the column from table2 under the name of the column in table1?
In MySQL what you specify in the select statement is what it is called in the result set, so you need to tell it what to give you. you do that with the AS command
SELECT users.id AS user_id ... FROM ...
That way you can call it whatever you want
or grab the user_id from news with
SELECT news.user_id ... FROM ...
SELECT users.id AS id1, news.id AS id2, news.title, users.username, news.contents
FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
echo $query['id1'] . $query['id2'];
I have to make a scores page for a game and one thing it has to display is the number of times the current user has played the game. Basically what i need to select ID and username from one table, link them together and count the corresponding user IDs from another table.
These are the tables I was given: Users --> ID, USERNAME and Times_Played --> ID, USER_ID
Thanks in advance
You want to query something like this:
SELECT Users.USERNAME, Times_Played.User_ID
FROM Users
INNER JOIN Times_Played
ON Users.ID=Times_Played.ID;
EDIT
After re-reading the question and seeing the need to count the times played you could do this via PHP or via your SQL Query. Via PHP:
$result = mysqli_query(
"SELECT Users.USERNAME, Times_Played.User_ID
FROM Users
INNER JOIN Times_Played
ON Users.ID=Times_Played.ID"
);
$timesPlayed = mysqli_num_rows($result);
If it has to be done via the query, Anish has the correct solution.
I hope that helps.
Try this
select count(*) as Times_Played,Users.USERNAME,Users.ID
from Users
inner join Times_Played
on Users.ID = Times_Played.USER_ID
I have a table with user payment details, and another table with a list of users. I want to get the balance of all users from the payment details, but only for users that are not banned (which is a column in the users table).
I am somewhat new to nested queries so I am not sure how to do this?
Here is what I have tried so far...
mysql_query("
SELECT SUM(balance)
FROM payment_details
WHERE (SELECT ban
FROM users
WHERE username=username
) != '1'
")
Note: Username is a column in both tables.
The above query does not work.
To Recap: There are two tables: payment_details and users. I want to add together the balance column for all the users that are not banned.
Don't use a subquery here. Instead, use a JOIN.
SELECT SUM(payment_details.balance) FROM payment_details
JOIN users ON payment_details.username = users.username
WHERE ban != '1'
Agreed, a join is probably what you want.
However to answer the specific question, you could try a query like:
SELECT SUM(payment_details.balance)
FROM payment_details
WHERE payment_details.username in (
SELECT users.username
FROM users WHERE ban != '1'
);
Note that it wasn't clear from the question whether you wanted the sum for each individual user, or the total sum for all users. The above query provides the latter -- not grouped by user.
the query is:
SELECT SUM(balance) FROM payment_details JOIN users ON payment_details.username=users.username WHERE users.ban !='1' group by payment_details.username
by the way, working on the users ids would be much better than on the usernames
I am looking for some help with a MYSQL query. I have two tables, one contains a list of tickets and the other is a list of members.
Table Tickets:
ticket_id,
member_id
Table Members:
member_id
A member can but doesn't have to be assigned to a ticket. What I would like to do is select all the tickets and then if the member_id field is set in the tickets table fetch the member information as well.
One approach is to do a SELECT * FROM Tickets and then loop through in PHP and check if the member_id field is set. If set then do another query on the members table to fetch the corresponding information. The only problem with this is that it would be a large number of queries.
Is there any way to fetch all the results in one join query?
Thanks
Select *
from Tickets
Left Join Members On tickets.member_id = members.member_id
select * from tickets left outer join members on tickets.member_id = members.ticket_id
SELECT t.ticket_id, t.member_id FROM tikcets t LEFT JOIN members m ON t.member_id = m.member_id
This will give you all the tickets whether they have been assigned to a member or not.
You should add to the query the additional fields that you want to fetch.
Try this:
SELECT t.ticket_id, m.member_id
FROM tickets AS t LEFT OUTER JOIN members AS m
ON t.member_id = m.member_id
The LEFT OUTER JOIN will cause all results from tickets to be returned, and any match from members, not disqualifying the ticket records, so this could do the trick.