Need help with mysql query - mysql

I am having a text-box which is auto complete.
When i enter the name on the text-box the auto complete will work and provide the list from table.
And when i click the save button the names which are entered in the text-box will inserted in a table. Until this, the process i working fine.
My case is : Already existing users should not show in auto-complete.
Table Structure :
Users Table
uid, name
work table
wid, from_uid, to_uid,
How the mysql query should be.. Any help regarding this will be thankful and gratedful...

select name
from users
where name like 'Jo%'
and uid not in (select to_uid
from work
where from_uid = 666)
Supposing that user entered Jo substring and current user's id is 666
or another query that does the same (not sure which one will be more efficient in your particular case) but uses LEFT JOIN instead of subquery:
select name
from users u
left join work w on w.from_uid = 666
and u.uid = w.to_uid
where name like 'Jo%'
and w.to_uid is null

Related

How do I write a query that includes users that aren't in the table at all?

I currently have this query set up:
SELECT age, hobbies, country
FROM profile
INNER JOIN matches
ON profiel.usernumber = matches.user1
WHERE age BETWEEN ? and ?
AND usernumber != ?
AND usernumber NOT IN (SELECT user2 FROM matches WHERE matches.user1 = ?)
FYI:
I'm using two tables.
Table "profile" has all the user info we need (age, hobbies, etc.).
Table "matches" has two user numbers of users that matched. Only if you click the "match" button you get inserted into the "matches" table with your user number and your match.
The problem:
The current query only selects users that are in the table "matches" but aren't matched with you. I also want the users that aren't matched at all (and thus aren't in the matches table)
I've tried multiple options with AND or OR but I haven't figured anything out myself. I'm new to coding so I'm grateful for any help.
INNER JOIN only show records that exists on both tables. Use LEFT JOIN instead.
SELECT age, hobbies, country
FROM profile
LEFT JOIN matches ON profiel.usernumber = matches.user1

MySQL query to find values in table with given value and find others that are similar

I have a table called followers and I want to be able to find the current users followers, display that list, and then compare those values against another user ID to see if they are following them or not from that same table
This is the table I'm using currently to get the list of followers for a specific user:
followers
-------
followId - primary key, the unique id for the follow relationship
userId - the user that is following someone
orgId - that someone that the user is following
I tried using a union query but I wouldn't want to due to performance reasons (table may contain high volume of records) and because it isn't scalable (I think)?
The expected output should be the list of orgId's for which the user(the user I am checking against) is following, and another column that shows whether my user(my userId that I provide) is following that orgId value (i.e a following column).
Hmmm, if I understand correctly, you have two users and you want to know which orgs followed by the first are also followed by the second:
select f.orgid,
(exists (select 1
from followers f2
where f2.userId = $seconduserid and
f2.orgid = f.orgid
)
) as seconduserflag
from followers f
where f.userId = $firstuserid

Is there a complicated select query to solve this problem

i need to make a query on a table and inside this queryi would like to replace the value of "created_by" in the value of corsspende user "first_name"
becaus a user create another user ,so i would like return the real name of creater instead of his id
i tried to get deeper in queries typess but the are so many ,inneer join, outer ,left join ,variable ,..
i don,t know which concept is the solution [enter image description here]
You'll perform an INNER JOIN from this table back to itself:
SELECT users.*, createdby.*
FROM yourtable users
INNER JOIN yourtable createdby
ON users.created_by = createdby.id
In order to do this the each table in the FROM clause gains an alias. The first time we use it I'm calling it users and the second time I'm calling it createdby. This makes it obvious in the rest of the SQL which table is being referred to and why.
We join on the created_by of the first table to the id of the second. Essentially the second table contains the attributes of the created_by user.
You'll just have to swap the yourtable out for your actual table name and probably swap those * out with the actual fields you want from each table.
This should be handled by client side code instead of query because it causes confusion. If you really want it, you can just take first_name column value and return it as create_by like showed below:
SELECT first_name AS create_by FROM user;

Mysql join where specific record in one table not in other

I've looked at other examples of "data from one table not in other" SQL but still can't figure this out.
Table "pictures" contains:
"id", an auto increment ID number for this picture
"owner", an ID number referring to a unique user
Table "ratings" contains:
"picture", a reference to an entry in the "pictures" table
"userby", an ID number referring to a unique user
I want to select all pictures which have no entry in the ratings table by a specific user AND where the picture owner is not that user.
For example I might want to select all pictures which user 5 has not rated and is not the owner of.
Usually this would be a join between pictures and ratings and check if the ratings record is null, but I can't seem to get the addition of doing it only for specific users right.
How can I do this? I want to avoid sub-selects if possible. Thank you.
You need to add the additional checks to the join predicate and not in the where clause.
So something like
SELECT *
FROM pictures p LEFT JOIN
ratings r ON p.ID = r.PictureID AND r.UserID = 5
WHERE r.ID IS NULL
AND p.OwnerID <> 5
Have a look e this example
SQL Fiddle DEMO
select *
from pictures as p
where p.owner <> 5
and not exists(select * from ratings where picture = p.id and userby = 5)
first select pictures which is not owned by user p.owner <> 5
then search ratings for that picture by user exists(subquery)
use not if need picture for which no rating shoul exists
Unfortunately result you need could not be produced by 1 step combination (without subselect), because to do so an operation required, that can combine something existent (any picture not owned by user ) with something nonexistent ( missing rating by user ).
If there were some table containing fact that user did not rate some picture, then it would be possible! SQL can operate with things that exists only. That is what not exists(subquery) do - it realizes fact that there are no ratings given by user to a picture.

How to avoid table for each user

I have a rather special use case in front of me. There is to be an excel file with around a thousand entries (rows), each row represents something that the USER should pass judgment on.
Now, the entries are the same for everyone. The data that should be collected is
a) how many users like any given entry
b) what entries does any given user like
Since part of the app is already running and we have user accounts,
I thought of creating a table for each user (!) containing said excel information, adding a row for collecting the votes. I would create those tables by iteratin through the user list and creating tables like "userid_excelentries".
I don't think that's elegant. I would prefer to store the excel information only once in a table and only save the users' votes in the table "user".
The app is meant to display a table created form the excel table (I have the grid already done) and a row next to it with checkboxes. How do I structure this ? Temporary tables ? How do I store the information what each user has selected in the "user" table, since I don't know how many selections will be made a-priori ?
I had this crazy idea of actually handling the xls object through javascript, serializing it into a hash and storing that hash into a field in each user's row...but I have no clue if this is sane :o
We're facing a user count of exactly 272 - this is why I considered doing the "one table for each user" approach.
You can use 3 tables in your DB
users table
-----------
id
name
...
entries table
-------------
id
name
...
user_entries table
------------------
user_id
entry_id
user_response
To get all entries a certain user (i.e. Tom) likes you can do
select e.name
from entries e
join user_entries ue on ue.entry_id = e.id
join users u on ue.user_id = u.id
where u.name = 'tom'
and ue.user_response = 'like'
And to get the count of likes for each entry you can do
select e.name, count(ue.user_id) as likes
from entries e
join user_entries ue on ue.entry_id = e.id
where ue.user_response = 'like'
group by e.id, e.name