I have a form set up where the user can enter in the username or lastname of a person. I then take either the username or lastname depending on what they entered and query the database and displaying all the userinformation and whether they have access to certain areas. There are two tables I need to query and get all the information for the user entered. I'm assuming you use a JOIN but I'm not quite sure how to do it. Right now I have the query to grab all the data from the users table I just need help adding in the second table. The two tables look like this
Users {
uid
username
password
firstname
lastname
email
}
userAccess {
uid
assessment
development
learningmodule
project
}
The SQL I have so far is
SELECT *
FROM Users
WHERE username = '$username' OR lastname = '$lastname'
You are right you'll need to use a join. This type of join is called an Inner Join. You can achieve it like this:
SELECT * from Users, userAccess WHERE Users.uid = userAccess.uid AND Users.uid = #;
I'm guessing you'll have an id column that is the Primary Key for your userAccess table as well. The above query will provide the data from both tables in 1 record for every condition where the above statement is true.
If you are using MySQL you can read more about joins here:
http://dev.mysql.com/doc/refman/5.0/en/join.html
You donĀ“t mention which database you are using, although I'm guessing MySql.
What you need to do is a simple inner join:
SELECT u.uid
u.username,
u.password,
u.firstname,
u.lastname,
u.email,
ua.assessment,
ua.development,
ua.learningmodule,
ua.project
FROM Users u
INNER JOIN UserAccess ua
ON u.uid = ua.uid
WHERE u.firstname LIKE '%$username%' OR u.lastname LIKE '%$lastname%';
Do note that I'm not selecting the uid again in the UserAccess table as it is redundant since you already selected it from the Users table and since you are joining by that key you don't need it again.
Also note that instead of using the equal operator (=) I use the LIKE operator since there are more chances that you want to filter information based on partial info such as keywords and not full words.
I'd recommend you to have a look at a SQL tutorial for beginners. This one seems to be good enough.
I'd also recommend you to read the guidelines on how to post questions here or else you won't get much attention from anybody, plus they will downvote you :)
Related
I am trying to create a booking system, I have a table for user to log in, another table for advocate login and another table for booking information. I want data entered from user to be displayed to a specific advocate. I have tried using SQL JOIN but it is not displaying any information.
MY TABLES
advocate
bookingcalendar
here is the SQL code
$sql = ("SELECT bookingcalendar.*,advocate.userName, FROM bookingcalendar JOIN advocate ON bookingcalendar.*=advocate.userName WHERE $current_epoch BETWEEN start_day AND end_day");
$result = mysqli_query($conn, $sql);
bookingcalendar.*=advocate.userName part is incorrect use bookingcalendar.Column_name where Column_name is common on both the table bookingcalendar and advocate which may be userName in your case
SELECT bookingcalendar.*,advocate.userName
FROM bookingcalendar JOIN advocate
ON bookingcalendar.userName = advocate.userName
WHERE $current_epoch BETWEEN start_day AND end_day
You need to use a field that is in common between your two tables as the key to the join condition. For example, with some kind of userID :
SELECT bookingcalendar.*, advocate.userName
FROM bookingcalendar
JOIN advocate ON bookingcalendar.userID=advocate.userID
WHERE $current_epoch BETWEEN start_day AND end_day
I advise you to not use the userName as the key to join your two tables, because multiple users can eventually have the same userName, and the join on two strings takes more time than the join on two numbers.
That is not just an advice to take lightly. You absolutely should join your tables on some numerical ID instead of a user name.
-- EDIT --
Actually, I don't really know what an "advocate" is, so maybe your join key shouldn't even be some user id/name. To know that, I need to better understand the relationship between advocate, user and bookingCalendar. Maybe a description of their columns could help us answer this hidden part of the question.
I am somewhat new to coding and have been trying to write what I thought would be a straightforward sql query. Please help :)
Table 1: Users
id = user idt
username = user name
Table 2: Orders
orderid = order id
order_to = user id of person buying
order_from = user id of person selling
oder_details = text
Basically I want to:
"Select Username(from), Username(to), order_details FROM mytables WHERE Order id = 1;"
And get the result as 1 row, I'm not sure how to proceed. I thought I could do this with concatenation or something... Can anyone help?
You need to use JOIN to link the tables together.
SELECT fu.username AS fromUser, tu.username AS toUser, o.order_details
FROM Orders o
INNER JOIN Users fu
ON fu.id = o.order_to
INNER JOIN Users tu
ON tu.id = o.order_from
WHERE o.orderid = '1';
Because you have two different users that you need the username from, you need to JOIN the Users table twice to get both user's usernames. Each table needs to have it's own alias fu and tu to allow MySQL to differentiate between them. The same goes for the column names in your SELECT statement so that when you fetch the results with PHP, php can differentiate between the two usernames.
You are looking for a JOIN. This can be done with a keyword or through WHERE clauses. For example,
SELECT * FROM Orders JOIN Users ON Orders.order_to=Users.id
The documentation can be found here: http://dev.mysql.com/doc/refman/5.0/en/join.html.
I'll leave it as an exercise to figure out how to JOIN the order_from.
I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields
I have 2 mysql tables
one has a list of user ids that are associated with a city. ie "Fort Lauderdale" but the user id is actually in a column called entity_id and the city is in a field called field_city_value.
This query brings back all of the entity_ids in "Fort Lauderdale"
SELECT entity_id
FROM `field_data_field_city`
WHERE `field_city_value` LIKE 'Fort Lauderdale'
and then this query brings back the mail for the user id
SELECT mail
FROM `users`
WHERE `uid` =42
I want to combine the 2 and get all of the mails for all of the user ids that match Fort Lauderdale.
Use a join statement.
http://dev.mysql.com/doc/refman/5.0/en/join.html
Heres the long winded way of doing it.. Untested, no mysql access on this box.
SELECT field_data_field_city.entity_id,users.mail FROM users,field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale' AND field_data_field_city.entity_id = users.uid
Or
SELECT * FROM field_data_field_city city INNER JOIN users user on city.entity_id=user.uid
Can we assume that users.uid is the same as field_data_field_city.entity_id? If that's the case you'll want to look into using MySQL joins
You can try a subselect:
SELECT mail FROM users WHERE uid IN (SELECT entity_id FROM field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale')
This should do it. You might want to change it a little bit for duplicates or similar, but this simple query should work
SELECT u.mail
FROM users u
LEFT JOIN field_data_field_city fdfc ON fdfc.entity_id = u.uid
WHERE fdfc.field_city_value LIKE 'Fort Lauderdale'
I have a list of usernames (with a column called "username") + other info in my "users" table.
I also have another list of usernames (with a column called "username") in my smaller "recentusers" table.
How can I create a mysql query to get all the usernames that are in the "users" table but not in the "recentusers" table?
I know it is probably very simple, but any help would be appreciated!
select username from users where username not in (select username from recentusers)
Then after the first username add in the other information you want to select as well.
As pointed out by OMG Ponies, this query will run as fast as using Left Join or Is null.
The NOT IN keyword should be helpful :
SELECT username
FROM users
WHERE username NOT IN (
SELECT username
FROM recentusers
)
SELECT * FROM users
LEFT JOIN recentusers USING (username)
WHERE recentusers.username IS NULL;
You can also use a left join (which will perform faster than a subquery):
SELECT
users.username
FROM
users LEFT JOIN recentusers
ON users.username = recentusers.username
WHERE
recenterusers.username is null
Joins should be faster than subqueries, though I do not really know if mysql supports this.
select * from users
left outer join recentusers
on users.key = recentusers.key