mysql how to avoid data duplication in select request - mysql

I have 2 tables:
USER: id, access_token
QUERIES: id, query, user_id, user_id is a foreign key
How to make a select request to return data in the next format:
{user_id: {[queries for this user], access_token}, ...}
Query:
SELECT USERS.id,
USERS.access_token,
QUERIES.query
FROM USERS
INNER JOIN QUERIES ON USERS.id=QUERIES.user_id;
and I have duplicating of users.id and users.access_token. I tried to use GROUP BY to get answer as in my example but group by doesn't help
Thanks.

This is just how SQL works. If you want to select from both tables in one query then you are going to get repeated data for columns from the users table.
SQL can only return a result set with the same columns for every row. So it has to put something there.
The most common way to deal with this is to loop over the data, if the user_id is the same as the previous row then don't output it to your user.

Related

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.

Normalise data into one table

I'm trying to insert rows into a table (usersteps) from the table steps for all users only if the step id does not exist.
INSERT INTO userssteps
(status,user_id,step_id)
SELECT
'0' ,
(SELECT DISTINCT id from users),
(SELECT DISTINCT id from steps)
I get the following error on the above MYSQL
#1242 - Subquery returns more than 1 row
Reason:
A new user signs up they should get all steps, if I create a new step i'd want to create it in usersteps for current users to see.
If there is a more clever way to do this i'd love to know but i'm stumped. I am also using cakePHP so if there is a special cakePHP way to help me in this i'd prefer that.
Table Structure
steps:
id
name
users:
id
username
password
userssteps:
id
user_id
step_id
status
It looks like you are trying to produce a cartesian product. http://en.wikipedia.org/wiki/Cartesian_product.
If there is no relations between the users and steps table then they cannot be joined, only multiplied.
INSERT INTO userssteps
(status,user_id,step_id)
select 0,
users.id,
steps.id
from users
inner join steps
The subquerys (SELECT DISTINCT id from users) and (SELECT DISTINCT id from steps) will return ALL the id's. In a insert clause you will need only one value (you can't have more than 1 value).
you can try to inner join the two tables by the ID
Try this way:
INSERT INTO userssteps
(status,user_id,step_id)
select 0 as status,
users.id,steps.id
from users
inner join steps
on (users.id=steps.user_id);
That way should works ;)
PS: Now the join is right.
Saludos.

Best way to get records from one table based on other table

I have 2 tables: twitter_followers and twitter_friends. Both tables have many columns (id, user_id, twitter_id, etc.). For a single user_id the number of rows in both tables can be more than 100000 records.
I want to retrieve records from twitter_friends of user in the following way:
SELECT *
FROM twitter_friends
WHERE user_id=1
AND twitter_id NOT IN (SELECT twitter_id FROM twitter_followers WHERE user_id=1)
This query is okay for small set of data, but can any one help me to get large no of data (preferably in a few seconds)?
MySql's subquery performance is shockingly bad. I would suggest using a JOIN statement.
Like:
Select Friends.*, Followers.twitter_id
from twitter_friends as Friends
LEFT JOIN twitter_followers as Followers
on Friends.USER_ID = Followers.USER_ID
where friends.user_id=1 AND followers.twitter_id is null;

MySQL: How to Select All Results That Don't Match LEFT JOIN

My SELECT statement is essentially:
SELECT *, user.user, response.survey FROM survey, user, response ORDER BY survey.added
However, I am trying to select only surveys that haven't been answered. The 'response' table contains 4 relevant columns (user ID [of user responding to survey], answer, question [same as survey ID if single question, if multi, corresponds to question ID], and survey ID)
I'm trying to write an SQL statement that selects all surveys that don't have a response from an arbitrary user ID ($_SESSION['userId'])... Tried going about this using various LEFT JOIN as well as nested SELECT commands, but haven't been able to figure it out.
Can anyone shed some light on how to go about this?
Or, just left join and check the column on the right for null:
SELECT *, user.user, response.survey FROM user
LEFT JOIN response ON response.user=user.user
LEFT JOIN survey ON survey.surveyID=response.surveyID
WHERE user.user=[userID from session] AND response.user IS NULL ORDER BY survey.added
Because if the matching column from the response table is missing, response.user would be NULL.
This Select statement you wrote:
SELECT *, user.user, response.survey FROM survey, user, response ORDER BY survey.added
Is doing a cross-join (getting all possible combinations of user,response and survey) and it's clearly not the way the data is referenced between the tables; therefore is wrong. You would need to join the 3 tables by a common key.
But to answer your question...
If there's a table with responses from a particular user; then do something LIKE this:
select * from survey s where s.survey_id not in (
select survey_id from response where userId=<particular_user_id>)
and s.user_id=<particular_user_id>
And that will return all surveys that the user has not responded.
I hope the idea is clear.

MySQL query to fetch most recent record

I've got four tables. The structure of these tables is shown below (I am only showing the relevant column names).
User (user_id)
User_RecordType (user_id, recordType_id)
RecordType (recordType_id)
Record (recordType_id, record_timestamp, record_value)
I need to find the most recent record_value for each RecordType that a given user has access to. Timestamps are stored as seconds since the epoch.
I can get the RecordTypes that the user has access to with the query:
SELECT recordType_id
FROM User, User_RecordType, RecordType
WHERE User.user_id=User_RecordType.user_id
AND User_RecordType.recordType_id=RecordType.recordType_id;
What this query doesn't do is also fetch the most recent Record for each RecordType that the user has access to. Ideally, I'd like to do this all in a single query and without using any stored procedures.
So, can somebody please lend me some of their SQL-fu? Thanks!
SELECT
Record.recordType_id,
Record.record_value
FROM
Record
INNER JOIN
(
SELECT
recordType_id,
MAX(record_timestamp) AS `record_timestamp`
FROM
Record
GROUP BY
recordType_id
) max_values
ON
max_values.recordType_id = Record.recordType_id
AND
max_values.record_timestamp = Record.record_timestamp
INNER JOIN
User_RecordType
ON
UserRecordType.recordType_id = RecordType.recordType_id
WHERE
User_RecordType.user_id = ?