using two tables with mysql - mysql

$sql = "SELECT * FROM `basic` WHERE id = 2 LIMIT 0, 30";
i want to retrieve data from 'users' table in the same query.
fields of users table are
1.country
2.name
3.userid
basic table consist of selling items and there are unique id for each one.
also users table consist of users and there are unique id for each one.
if i say i want data from basic table where id is 4 and user's table where id is 15 in same query....how should i rewrite above mysql statement

May be you need to read this JOIN.Try to read some tutorials that can help you improving your programming knowledge.

$sql=SELECT *
FROM `basic` as b inner join `table2` as c
WHERE b.id =c.id

Related

I'm trying to let a user who has been assigned something be able to view it with PHP and MYSQL

I've tried searching an answer for this and I can't realy find the one that fits what I'm looking for.
I'm making a small web application that allows an admin to create course material and a test, that a user will be assigned.
What I am trying to figure out, is how a user, who has been assigned a course, can access that course by clicking a button.
I have created one table which stores the user information, with a user ID being a primary key. I have a course table, which stores all materials and the course's test, with a course id being that table's primary key.
I made a third table which is an assigned_courses table, it contains two foreign keys. One referencing the user id from the user table, and one referencing the course id on the course table.
I've inserted a few images to show what you mean.
I can't quite figure out the Sql syntax to pull course materials and test based on the assigned_course table.
All feedback appreciated. This is the two foreign keys that reference the user id (id) and the course id(u_id)
Not sure what column names you have so I used * instead. You can use JOIN (AKA INNER JOIN) to link the tables together. You join on the primary keys of the 2 tables.
I have created one table which stores the user information, with a
user ID being a primary key. I have a course table, which stores all
materials and the course's test, with a course id being that table's
primary key.
I used id for your user table and course_id for the course table. Change as needed.
Find all courses for a given user.
SELECT a.*
FROM course a
JOIN assigned_course b ON a.course_id = b.id
WHERE b.u_id = :userId
;
Find all users for a given course.
SELECT a.*
FROM user a
JOIN assigned_course b ON a.id = b.u_id
WHERE b.id = :courseId
;
EDIT #1 - Subquery without JOIN
Thanks for your feedback, as I stated above, I was hoping to avoid a
join.
I highly recommend you learn joins. They are essential when you need to query more than 1 table. In this case, you can use a sub-query to find matching courses for a user but you won't be able to access that user data at the same time; that is the power of a join. I'll include a final example underneath showing how to get courses and user data at the same time.
Find all courses for a given user (using sub-query)
SELECT a.*
FROM course a
WHERE course_id IN (
SELECT id
FROM assigned_course b
WHERE u_id = :userId)
;
Final example showing courses related to users (many-to-many)
SELECT * -- You now have access to a, b, and c data
FROM course a
JOIN assigned_course b ON a.course_id = b.id -- joined by PKs
JOIN user c ON c.id = b.u_id -- joined by PKs
WHERE c.id = :userId
;
It sounds to me like you want to use SQL Join syntax which goes as follows:
select column1, column2, column3 etc...
from table1
join course on table1.id = course.id
join assigned_course on table1.id = assigned_course.id
replace the column1,2,3 etc with the actual column names, and replace the foreign_key_name with the actual name of the foreign key in the sub-table and the same for the user_primary_key
you can find some other examples of mysql joins here
as far as how to implement them into your html or php file, that is a very different question and would require knowing alot more about how your code is setup in the form
a place that is pretty user friendly is that might help with pulling the data is here just make sure you are aware of using prepared statements for your code

Fetching datasets from crosslinked table in a single query

Imagine the following MySQL table environment:
admins
id username
usergroups
id groupName
adminXusergroup (crosslinking table)
id adminId groupId
adminId and groupID are indexed and foreign key relations are configured accordingly (adminId points to admins.id and groupId points to usergroups.id).
All id fields are the primary keys.
I would like to retrieve all datasets from usergroups that correspond to a specific user in admins.
I have managed to do this in PHP by first fetching the according rows from adminXusergroup and then creating a list of IDs to select from admins in a second call. But even though it works, i feel like it can be optimized further, possibly by fetching all required information in a single MySQL query (joins perhaps?).
How can i fetch all datasets from usergroups that correspond to a specific dataset in admins (based on the id) by incorporating the information in the crosslinking table adminXusergroup ?
My current approach started like this:
SELECT * FROM usergroups WHERE usergroups.id=(SELECT id FROM adminXusergroup WHERE adminId="1");
But it doesn't seem right, and i am not sure as to how to apply nested selects in such a case?
I think you can achieve this using IN condition
SELECT *
FROM usergroups
WHERE usergroups.id IN
(
SELECT id
FROM adminXusergroup
WHERE adminId=1
);
Otherwise you can use an INNER JOIN query
SELECT *
FROM usergroups a
INNER JOIN adminXusergroup b
ON a.id = b.groupId
WHERE b.adminId = 1
Try This
SELECT a.id,u.*,x.* FROM admin as a
INNER JOIN usergroups as u on a.id=u.id
INNER JOIN adminXusergroup as x ON x.id=u.id WHERE u.id='userid'

Query to merge two tables based on some criteria

Table users includes columns first,last, and company.
Table columnscontains 5 columns containing different information and a company column.
I was wondering if there is a way to select users record based on the criteria below AND select the row in the columns table that contains the same company name as he comapany column in the users table?
I appreciate any suggestions
Something like:
$st = $this->db->prepare("SELECT * FROM `users`,`columns` WHERE `first`=? AND `last`=? AND `users.company` = `columns.company`");
Assuming that the companies are unique, you can use a LEFT JOIN:
SELECT [column list]
FROM `users` u
LEFT JOIN `columns` c
ON c.`company` = u.`company`
WHERE u.`first` = ?
AND u.`last` = ?

MySQL selecting tables where user exists

Let's say I have a mySQL table called "user" that contains a userid and a password or something. Then I have other tables for different purposes, such as a table for their favorite food, which would contain their userid and favorite food.
Is there a way to query for tables that contains a specified userid and checks whether they have an entry in that table?
So if I have three tables called favorite_food, favorite_drink, and favorite_candy, but userid only had values in favorite_food and favorite_drink, I want the query to return favorite_food and favorite_drink.
I'm a beginner and I couldn't quite grasp the concept of linking. Userid is the primary key of user and the other tables reference it?
Read more about UNION. I think that this is what you need.
SELECT * from favorite_food where `userid` = 50
UNION
SELECT * from favorite_drink where `userid` = 50
UNION
SELECT * from favorite_candy where `userid` = 50
In SQL "linking" is represented by JOINs. Since (as you indicated) some users will not have 1 or 2 favorite objects we will need to use LEFT OUTER JOINs. Then we will have NULLs in the output in place of missing favorites.
In your case:
SELECT u.userid, f.food_name, d.drink_name, c.candy_name
FROM user u
LEFT JOIN favorite_food f ON u.userid=f.userid
LEFT JOIN favorite_drink d ON u.userid=d.userid
LEFT JOIN favorite_candy c ON u.userid=c.userid
ORDER BY u.user.id
This will give you favorites for all users in your user table.

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;