Is it possible to limit a row in MySQL? - mysql

The problem is that when I echo user information, it also echos a column that multiple times even though it has one value. I want to echo out the maximum value of that column instead of doing it multiple times depending on other columns.
$sql = "SELECT * FROM users JOIN user_images ON users.username = user_images.username WHERE users.username = '$username' GROUP BY user_images.username";
The users table have one column with one value like a profile picture. The user_images table have one column with multiple values like posts. I also researched that GROUP BY should help with this problem, but it seems to be not working. Is there a way to just echo the maximum of rows or limit the other one?

You need to replace the * in the SELECT * bit of your query so that you are only selecting one value per column that is not in your GROUP BY clause.
For example:
SELECT user_images.username, MAX(user_images.posts)
FROM users
JOIN user_images
ON users.username = user_images.username
WHERE users.username = '$username'
GROUP BY user_images.username";
Will return each username and the max of the posts column corresponding to that user. You can add more columns to your SELECT statement as long as you're only returning one record per your GROUP BY
variable.
To return two tables for your desired outcome:
One table which is one column of posts for the specified user:
SELECT posts
FROM user_images
WHERE username = '$username';
And a second one for that username and profile image:
SELECT username, profile_image
FROM user_images
WHERE username = '$username'
GROUP BY username, profile_image;
You don't need the joins you were doing if all of these columns are in the user_images table. If you also want columns from the users table you can add the join back in.

Related

mysql- Display a list of users sorted according to the time

I have a page wherein I would like to display the list of user who have signed up for the paid subscription.
The list should be sorted according to the subscribed_time row present in another table called paid_subs.
This is the query I am using
SELECT *
FROM user
WHERE EXISTS
( SELECT *
FROM paid_subs
WHERE paid_subs.user_id= user.id
ORDER
BY paid_subs.subscribed_time DESC)
What the above query is doing is simply giving me the list of users who have a paid subscription.
I would like the query to display the list of paid subscriber in a subscribed_time sorted order.
You need to do a JOIN between the two tables on user_id. Inner Join will ensure that only those users are considered, which have paid for the subscription.
Now, do a simple Order By to sort the data based on subscription time.
Also, you should fetch only specific fields (as per your application code requirements), instead of using wildcard (*). Do read: Why is SELECT * considered harmful?
Use the following query:
SELECT u.* -- please change this to specific fields, eg: u.id, u.name etc
FROM user AS u
INNER JOIN paid_subs ps ON ps.user_id = u.id
ORDER BY ps.subscribed_time DESC
Exists simply check if the corresponding records exists and returns a true or false result. If you want to order by a field from another table, then you must use a join.
SELECT * FROM user
INNER JOIN paid_subs ON paid_subs.user_id= user.id
ORDER BY paid_subs.subscribed_time DESC

MySQL Join statement to get data from two tables into a datagridview

I have two tables that I'm trying to join, 'holidays' and 'users'.
Users contains all my user info, the the column 'id' being primary and unique.
Holidays contains a column called 'userid', which corresponds to the id in the user table.
I'm struggling to get the join statement to work... what I'm looking for is the result of the select statement to give me the friendlyname (column 'fname' in user table) instead of giving me the value of userid.
Here's what I'm trying...
SELECT * FROM holidays JOIN users on users.id=holidays.userid WHERE holidays.status = 0
But i'm not getting a correct result - SQL executes without error, but my DGV is filled with tons of erroneous results.
Apologies If I have not used the correct terminology or whatever.
I'm new to the concept of joins.
Here is hopefully a better explanation of what I am after...
Thanks in advance.
You need to select the specific values you want from every table in the JOIN:
SELECT u.fname
FROM holidays h
JOIN users u
ON u.id = h.userid
WHERE h.status = 0
by the alias (FROM users u) you can select column from users table by u.fname
First try to right join to the User table. If you just want the fname then select the column name in the SELECT query, as SELECT * takes more time then SELECT column name.

Getting the number of posts that a user has posted?

How can I get the number of posts that a user has posted using one MySQL query?
I can't really think of anything but this, but there is no aggregate function on the join. So I'm not sure how to proceed. I am positive that joins will not accomplish what I need.
select a1.username as Username
from `logs` as a1
left join `logs` as a2
on a1.username = a2.username
For example, my logs table is filled with information about posts people have made. I want to find how many posts each user has made, i.e.
Username, Posts
User1 100
User2 200
etc
EDIT: Sorry for not providing enough information.
I have a table called logs and it has two columns. One column is called username and another column is called msg. It basically holds information about posts that people have posted.
For example, let's say someone named Red posts Hello world. It will be saved to the table logs and a new row will be created. username will be Red, and msg will be Hello world
I basically want to get the number of messages that EVERY SINGLE user has posted by their username. I.e. here is an example of what I want
Username Posts
Red 1
Blue 10
Sally 30
try this
SELECT Username, count(Posts)
FROM `logs`
GROUP BY Username;
Good luck.
I'm assuming that when you say you "can't use count(*) in a join", you mean that you tried and saw that it didn't work, rather than you can't use COUNT at all. So I'm using it here.
You're right that a JOIN is the wrong place for a COUNT. You want it up in the SELECT column list, and a GROUP BY down below. Aggregate by Username, and count the number of entries in each aggregate.
SELECT Username, COUNT(*) AS Count
FROM logs
GROUP BY Username
This query may help you, change this query as for your requirement
SELECT users.*, count( posts.user_id )
FROM posts LEFT JOIN users ON users.id=posts.user_id
GROUP BY posts.user_id
may be like
SELECT users.username, count( logs.username ) as posts
FROM users LEFT JOIN logs ON users.username=posts.username
GROUP BY users.username

Query to merge two tables based on some criteria

Table users includes columns first,last, and company.
Table columnscontains 5 columns containing different information and a company column.
I was wondering if there is a way to select users record based on the criteria below AND select the row in the columns table that contains the same company name as he comapany column in the users table?
I appreciate any suggestions
Something like:
$st = $this->db->prepare("SELECT * FROM `users`,`columns` WHERE `first`=? AND `last`=? AND `users.company` = `columns.company`");
Assuming that the companies are unique, you can use a LEFT JOIN:
SELECT [column list]
FROM `users` u
LEFT JOIN `columns` c
ON c.`company` = u.`company`
WHERE u.`first` = ?
AND u.`last` = ?

mysql: two tables but three select criteria

I have two tables: users and user_depts. Let's say (for this question) that users only has an 'id' column, and user_depts has 2: 'uid' and 'did'.
I am looking for an SQL line that will return all the user IDs for all the departments with which a given user ID (let's say 7, though this'll come dynamically from PHP) is associated.
What I've tried is:
SELECT id FROM users, user_depts
WHERE users.id = user_depts.uid
AND user_depts.uid = 7
But of course this does nothing but return 7. I think I might have to join the table to itself, but I only know the shortcut syntax for joining, and it doesn't seem to be sufficient. Any pointers would be greatly appreciated!
Use EXISTS:
SELECT uid FROM user_depts
WHERE EXISTS (
SELECT * FROM user_depts a
WHERE uid = 7 AND a.did = user_depts.did
)
I am looking for an SQL line that will return all the user IDs for all
the departments with which a given user ID (let's say 7, though
this'll come dynamically from PHP) is associated.
If this means you want to find all the users with the userid: 7 that has a user_departent connected to it this is your query:
select users.id from users
inner join user_depts.uid = users.id
where users.id = 7
select uid from user_depts where did
in (select did from user_depts where uid=7)
First select all the did with which a user is associated using subquery
Then select all the user_id for the selected departments.
It's the best way you can do it.
and if you want to remove repeated result then you can use distinct