I am building a web app that uses a system similar to twitter (Follow someone and that users ID is added to your profile and when you view your timeline you see all of your follows). I am trying to select multiple fields of data in one statement with different conditions. i.e. SELECT * FROM TableName WHERE id='I want to have multiple IDs for the query to select.'. Each user is different so I am wondering do I just separate them by commas or something? I have no idea. Thanks in Advance.
If I understand what you mean correctly, you're looking for the IN keyword that will match any listed id;
SELECT * FROM TableName WHERE id IN (1,7,12,44,36);
Use "WHERE id IN (1, 2, 3)"
For example.
You can use IN LIST function like SELECT * FROM TableName WHERE id IN (1,2,3,4,.....)
as stated, you are looking for the IN clause... however, I think this is really a JOIN to your 'friends' table instead.
SELECT *
FROM tableName
WHERE ID IN (1,2,3, ...)
Related
How to select data from two columns in same table in a single query.
for example select name "waseem" from username if not found check in city row.
select * from <table_name> where name like 'waseem' or city like 'waseem';
I agree with other's answers but with slight modification. If there is a record waseem ahemed and your only looking for waseem wont be searched in other answered query.
You need to use the wildcard % if you want to search for waseem.
I highly suggest you not to use * for selecting the column names because of the performance issues.
SELECT name, username, password WHERE name LIKE '%waseem%' OR city LIKE '%waseem%'
This is working for me
SELECT * FROM table WHERE row_name1 LIKE 'value' OR row_name2 LIKE 'value'
I am making a small php website in which you can follow others and then see their post.
I have three tables-
1.Posts, which has post_id and author_id
2.follow, which has following and follower
3.users, which has id, username, and all other stuff. I try the following in sql-
SELECT * FROM posts,follow,users WHERE posts.author_id=users.id AND users.id=follow.following AND follow.follower='$id' UNION SELECT * FROM posts,users WHERE posts.author_id=users.id AND users.id='$id'
Where $id is the id of the user logged in.
It displays the following error-
#1222 - The used SELECT statements have a different number of columns
I have searched for hours but I cannot find the answers to match with my query.
I will really appreciate an answer with a better version of the above code.
Thanks in advance.
Perhaps a JOIN would serve you better ... something like this:
SELECT * FROM posts
JOIN users on posts.author_id=users.id
JOIN followers on users.id=follow.following
WHERE follow.follower='$id'
When you union two queries together, the columns on both must match.
You select from posts,follow,users on the first query and posts,users on the second.
this won't work.
From the mysql manual:
The column names from the first SELECT statement are used as the column names for the results returned. Selected columns listed in corresponding positions of each SELECT statement should have the same data type
I have to search the database for certain results which are in one table but are not in a certain column in another table. my query below is what I have so far.
$sql = "SELECT * FROM users WHERE name LIKE '%".$_POST["search"]."%' AND NOT IN
(SELECT friend FROM friends WHERE user='{$user_name}')";
as you can see I have the WHERE and then I declare the first item it has to search for. But then I add in AND so I am can search another table which is in the brackets.
What is the best way I can do this action without having to change my format to much?
if anyone has any questions please post them below. So I can improve my question quality in the future please post below any ways that I can improve.
The problem isn't the use of two WHERE clauses, it's that instead of writing:
... WHERE name LIKE '%xxx%' AND NOT IN (...) ...
you have to write:
... WHERE name LIKE '%xxx%' AND name NOT IN (...) ...
"SELECT * FROM users left join friends on users.name=friends.friend WHERE friends.friend IS NULL AND users.name LIKE '%".$_POST["search"]."%'"
First of all, I am new here and just learning sql, so bear with me.
I have a simple question. Let's say I have a table called voting with the following columns:
id, token(int), candidate(int), rank(int)
.. and I want to perform a query like:
SELECT *
FROM voting
WHERE rank > t1.rank
AND token = t1.token
.. where t1 is
SELECT rank,token
FROM voting
WHERE candidate = $mycandidate
How can I do this in a single statement using AS alias, or whatever is the simplest method?
Please note that final table created may have rows with different candidates than i have specified i.e the rank,token variables are initially chosen according to candidate but once chosen they may contain any candidate value with them.
Something like this should do, I believe:
select
v1.*
from
voting v1 inner join
voting v2
on v1.token = v2.token and
v2.candidate = $mycandidate and
v1.rank > v2.rank
EDIT changed SELECT * to SELECT v1.*
I know WHERE, LIKE, IN filters in database. But how can I filter with "except" ? I want to select all data except a specificdata.
Thanks in advance
with NOT IN, usage is like IN
SELECT * FROM USERS WHERE ID NOT IN (1,2,3,4,5)
Three approaches:
WHERE column <> 'something'
WHERE column NOT LIKE 'something'
WHERE column not in('something')
you can use NOT IN
Note that you can use another query for NOT IN like this:
SELECT id FROM customers WHERE id NOT IN (SELECT id FROM bad_customers)
WHERE NOT LIKE
WHERE NOT IN