Yes so im building an query from the advanced search form.
I have this:
$query = "SELECT * FROM users WHERE 1 ";
$query .= "AND sex = '$sex' ";
for now, next im going to have AND birthday.. but then i dont know how to do it, because users birthday is stored in users_profile
So please correct me, how can i:
$query .= "AND birthday in users_profile = '1'";
Is this possible even, or should i reconstruct it and put birthday in users instead..
update:
in users, the id column there, is binded with users_profileĀ“s uID.
So in users_profileĀ“s uID column, there is the users id.
I assume your users_profile table is linked to the users table?
SELECT u.*, up.birthday
FROM users u
INNER JOIN users_profile up
ON u.user_id = up.user_id
WHERE sex = '$sex'
Here an Inner Join is used. The reason we can use u instead of users and up instead of users_profile is because we have set up the aliases "users u" and "users_profile up"
You need to look at the syntax for JOIN.
You need a way to join individual related rows in the two tables, something like:
SELECT u.* FROM users u, users_profile p
WHERE u.sex = 'M'
AND p.birthday = '1'
AND u.userid = p.userid;
I don't understand why you have separate tables for user and for users_profile, but you need to JOIN the two tables:
SELECT U.*
FROM users U
LEFT JOIN users_profile P
ON P.uID = U.uID
AND P.birthday = '1'
WHERE U.sex = '$sex'
Very possible, given you have the foreign key to the users_profile table.
Let's say the primary key in the users table is named 'id', and the users_profile table contain a field called 'uid' which point to the users table, you'd normally create the query like this:
SELECT * FROM users u, users_profile p WHERE u.id = p.uid
AND u.sex = '$sex' AND u.birthday = 1
Related
If I want to make an update query using INNER JOIN, how do I do in this case:
I have a users table & user_settings table. users contains column id and user_settings contains column userid. I want to do like:
UPDATE users_settings SET commets = 0 WHERE email = "$email";
But user_settings does not contain email, so I want to pick the email from the users table where the id is userid.
You can use this query. it will work
UPDATE users_settings SET commets = 0 WHERE userid
IN (SELECT id FROM users WHERE email = "$email");
The following will be helpful
UPDATE US SET US.Comments = 0
FROM User_settings US, Users U
WHERE US.Userid = U.id
AND U.email = 'emailid'
JOINs are slightly better in terms of performance than SUB QUERIES. Reference here.
So instead of using this -
UPDATE users_settings SET commets = 0 WHERE userid
IN (SELECT id FROM users WHERE email = "$email");
I would suggest this using INNER JOIN -
UPDATE users_settings
SET commets = 0
FROM user_settings
INNER JOIN users
on users.id = user_settings.userid --JOIN CONDITION
WHERE users.email = "$email" ; --SELECTION/SLICING CRITERION
Hope this helps.
Try this:
UPDATE user_settings s
INNER JOIN users u ON s.userid=u.id
SET s.commets = 0 WHERE u.email = "$email";
Note: There should be no users with the same e-mail in these cases.
First I want to write a join query to get every doc and the subjects that he uploaded i used this query but it is not working well and showing all the subjects under one doctor and all i have to get the subjects of the course is the course ID
SELECT users.ID
, fullname
, Sub_ID
, Sub_name
, Sub_ext
, Sub_path
, subject.created_at
FROM users
JOIN subject
ON users.ID = subject.ID
WHERE C_ID = '$C_ID'
Your query is wrong...
Correct is...
"SELECT u.ID,u.fullname,s.Sub_id,s.Sub_name,s.Sub_ext,s.Sub_path,s.Created_at
FROM users u LEFT JOIN subject s
ON u.ID=s.ID
WHERE s.C_ID='.$C_ID.'";
i solved it by using 2 queries
first one to get doc in that course and the second one from the subjects that doctor give
$query = "SELECT `doc-course`.ID,fullname
FROM `doc-course`
JOIN users ON `users`.`ID` = `doc-course`.`ID`
WHERE C_ID='$C_ID' ";
the second
$query2 = "SELECT Sub_ID,Sub_name,Sub_ext,Sub_path,`subject`.`created_at`
FROM subject
WHERE `subject`.`ID` = '$ID[$x]'
";
I have two tables: publick_feed and users
I want to SELECT all from public_feed and also SELECT a three columns from users whose id is the same of user_id in public_feed
and assign the rows returned from public_feed to the column in users table ( correspondent)
I try this:
<?php
$sql = "
SELECT * FROM public_feed
WHERE user_id IN
(SELECT id FROM users) AND
(SELECT Firstname,Lastname,Avatar FROM users WHERE id IN(SELECT user_id FROM public_feed))
";
$query = mysqli_query($dbc_conn,$sql);
if(mysqli_num_rows($query) > 0){
while($row = mysqli_fetch_assoc($query)){
//echo rows with correspondent details from the users table
echo $row['user_id'];
}
}
<?
Please any help will be much appreciated.
Thank you.
Or version with left join in case if there is no user in public_feed, and you still want to fetch user data
SELECT
u.*, f.*
FROM
public_feed f LEFT JOIN
users u ON f.user_id = u.id;
Because author asked for explanation, here it is:
First we are going to use table name alias to make query shorter
public_feed f
and
users u
we are saying that want to refer to tables with an alias. Of course * means that we want to select all columns
SELECT users.*, public_feed.*
is equal to
SELECT u.*, f.*
Of course you can use any other letters as an alias
Next we are saying that public_feed.user_id must be equal to users.id. But when public feed entry does not exists just display columns with null values. This is why we are using LEFT JOIN instead of INNER JOIN. In general JOINS are used to fetch related data from more than one related tables.
ON keyword is saying values from which columns in the tables must be equal to satisfy the request
I think doing a join would be cleaner than using a complicated subquery:
SELECT u.Firstname,
u.Lastname,
u.Avatar,
COALESCE(pf.User_id, 'NA'),
COALESCE(pf.Post, 'NA'),
COALESCE(pf.Date, 'NA')
FROM users u
LEFT JOIN public_feed pf
ON u.Id = pf.User_id
I chose a LEFT JOIN of users against public_feed on the assumption that every feed will have an entry in the users table, but not necessarily vice-versa. For those users who have no feed entries, NA would appear in those columns and that user would appear in only a single record.
I have 2 tables I am struggling to join information I think I am overlooking something.
users
-----
uid username rank_id reg_date country
mil_rank
-----
rank_id rank_title
Desired output:
---------------
Username Rank Title Reg Date Country
john sgt today usa
$sql = "SELECT * users.uid, mil_rank.rank_title ".
"FROM users, mil_rank ".
"WHERE users.rank_id = users.rank_title";
I would like to select all from both tables if possible
There are apostrophe " and points . too. Here's the good syntax
$sql = "SELECT users.uid, mil_rank.rank_title
FROM users, mil_rank
WHERE users.rank_id = users.rank_title";
You need to use the rank_id key to tie the two tables together:
SELECT users.username, mil_rank.rank_title, users.reg_date, users.country
FROM users, mil_rank
WHERE users.rank_id = mil_rank.rank_id
Since the rank_id column is present in both tables and you don't want that or the uid column in your output you probably also don't want to just select *.
Try with this example
SELECT u.uid, u.username, u.rank_id, u.reg_date, u.country, m.rank_id, m.rank_title
FROM users u, mil_rank m
WHERE u.rank_id = m.rank_id
If you wish to use a JOIN then this is how you would write that query
SELECT u.username, u.reg_date, u.country, m.rank_title
FROM users u
JOIN mil_rank m ON m.rank_id = u.rank_id
If you want this for a specific rank then
SELECT u.username, u.reg_date, u.country, m.rank_title
FROM users u
JOIN mil_rank m ON m.rank_id = u.rank_id
WHERE m.rank_title = 'sgt'
In answer to your comment, Yes
SELECT u.username, u.reg_date, u.country, m.rank_title
FROM users u
JOIN mil_rank m ON m.rank_id = u.rank_id
WHERE u.status_id != 1
Maybe if you read through a Basic SQL Tutorial you would be able to do the basic stuff yourself.
I'm trying to select all users that their user id is not in another table.
Users table (user id is user_id)
Ghosts table (user id is id)
this is what I got so far:
$sql = "select * from users where users.user_id <> ghosts.id;";
Please advise...
Cheers!
You can use NOT IN:
select * from users u where u.user_id not in (select id from ghosts);
or NOT EXISTS:
select *
from users u
where not exists (select 1 from ghosts g where g.id = u.user_id)
Something like this may also work, although I like Brian's answer.
SELECT *
FROM users u
LEFT JOIN ghosts g
ON u.user_id = g.id
WHERE g.id IS NULL