mysql Search data from multiple table - php

I have 2 tables:
user
ID | Name | Class
Category
ID | user_id | cat_id
If user inputs data from a text field how do I search data from both tables?

You will need a basic query with joins. Something like this:
SELECT * FROM user u
LEFT JOIN category c ON c.user_id = u.id
WHERE ...

SELECT * from user, category
WHERE user.id=[text field]
or category.user_id=[text field]
or category.cat_id=[text field]

You need to join the two tables together.
Select *
from User , Category
where user.id = Category.user_id
Basicly you are linking the two together by the based upon the user_id that they both share. This way you get the information back from both tables.
Here is a link to help you understand the concept. http://www.w3schools.com/sql/sql_join.asp

Try this way
SELECT * FROM user
LEFT JOIN category ON category.user_id = user.ID
WHERE user.Name LIKE '%lorem%'

Try it
you need the time of insertion use mysql_insert_id for user_id to table2.
In the selection time use JOIN in mysql
eg
"select * from tb1,tb2 where tb1.ID=tb2.user_id and where tb1.ID='userid' "
for a specific user

Even you can do like this:
SELECT * from user
LEFT JOIN category ON user.id = category.user_id
WHERE text_field IN (user.id,category.user_id,category.cat_id)

Related

Want to select data from different tables

I have two tables in database, named users(store user details) and posts(store post details). Now i want to get data from both tables. Like user_image from users and post description from post.
I am using this query
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`)
But it returns duplicate data. I have 2 users and 3 posts but it returns 6 posts.
Try something like:
Select a.user_image, b.post_description from users as a join posts as b on a.user_id = b.user_id
Do an inner join & you shall get the desired result
In the above query a & b are alias for the two different tables. I you do not want to use alias you can also write it as users.user_image in your select statement.
Write the fields you want from both the tables in your select statements.
The below image will help you understand the inner join
Inner Join Circle for understanding
Use group by as below:
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`) group by u.user_id
What about
Select * FROM user u right join posts p on u.id = p.user_id
?
if you want to get data from both tables you need to use joins.and you make sure the two tables are interlinked by primary keys
so use this can help
select user_image,post_description from users join posts on users.user_id=posts.user_id;

MySQL - How to get data from 3 tables

I seem to be a little stumped... I'm trying to get data from three tables, but they're not all inter-related -- one table relates to each of the other two.
Exams_taken
ID
exam_id
user_id
Exams_available
ID
exam_name
Users
ID
user_name
I want to create an output where I have the exam_id, exam_name, and user_name.
I thought I could figure out how to do this as a single query, but I'm lost. Is it possible? Or do I need to do a query on 'Exams_available' and then a loop with a second query to JOIN 'Exams_taken' and 'Users'?
Thanks,
Scott
If you need an output that contains exam_id, exam_name and user_name I can suppose that you need the Exams taken, so why not just query like this:
SELECT exam_id, E.exam_name, U.user_name FROM Exams_taken as ET
INNER JOIN Exams_available as E on ET.exam_id = E.exam_id
INNER JOIN Users as U on ET.user_id = U.user_id

How to transfer a lot of values beteen tables in a variable or array?

I have two SQL tables. The first one structure is simple.
ID Name
----------
The second one is simple too.
CommentID Comment CreatorID
----------------------------------
I want to show in my site all the comments that correspond with the "ID of user"
The problem is we have here a lot of ID and two different tables.
Something like this:
$1= $bdd->query("SELECT * FROM comments WHERE id_user=220281");
$2=$1->fetch();
But its impossible because id user is not on the comments table.
The most simple way to do this is to join the tables like this:
select
users.name,
comms.commentID,
comms.comment
from
userTable users
join commentTable comms
on users.ID=comms.ownersID
where
users.id=1
This will return the users name in each row of data, but you don't need to use it in the output more than once.
It also sounds like you could use a few pointers on SQL queries. Do yourself a favour and have a read of this article I put together a while back.
SELECT c.*
FROM comments c
INNER JOIN users u ON c.id_creator = u.id_user AND
u.id_user = 220281
A simple join will do the trick like this :
SELECT c.comment, u.user_name FROM
Users u
JOIN
Comments c ON
c.creator_id = u.user_id
WHERE
u.user_id=220281
fiddle:http://sqlfiddle.com/#!6/3b28a/1

Calculate sum from two Tables for a specific User

i have an Table with Users and an Table with events that is related to the id from the Users Table. In each Event you can make points and i want to get the total points from an specific User.
It looks like this:
Users
+----+----------+
| id | username |
+----+----------+
Events
+----------+---------+--------+
| event_id | user_id | points |
+----------+---------+--------+
// The event_id is related to an Event Table with specific data about the Event. but that not relevant.
The best could be to get the data from the user and the total points that he got in one query.
Thanks and Greetings,
Mottenmann
"..to get the data from the user and the total points that he got in one query."
You need to join both tables first so you can manipulate the data. The query below uses INNER JOIN which only includes users on the result list if it has atleast one matching record on the Events. If you want to get all users even without a single matching record on the other table, use LEFT JOIN instead.
SELECT a.ID, a.username, SUM(b.points) totalPoints
FROM Users a
INNER JOIN Events b
ON a.ID = b.user_ID
GROUP BY a.ID, a.username
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
"In each Event you can make points and i want to get the total points
from a specific User."
You could do something like this:
select sum(e.points) as points from users u
left join events e ON (u.id = e.user_id)
WHERE u.id = {$id}
where {$id} is the id of user.

Using the MySQL select statement

I would like to select the posts from users based on who the logged in user is following. What would I need to do? How do I use two different tables with one SELECT statement? I don't even know where to start.
I have 3 tables:
users
posts
followers
Thanks.
SELECT p.*
FROM followers f
JOIN posts p
ON p.author = f.following_id
WHERE f.user_id = $logged_in
ORDER BY
p.post_date DESC
I had to make up the field names as you haven't provided them.
Selecting from two tables is done using JOINs
http://dev.mysql.com/doc/refman/5.0/en/join.html
basically you select from two tables and define JOIN condition.
Assume you have two tables:
users with columns: user_id, user_name, online_state
posts with columns: post_id, user_id (user who posted this post), title, message
SELECT p.title, p.message FROM users u JOIN posts p ON u.user_id = p.user_id WHERE u.online_state = 'online'
join condition should be after ON, non-join condition after WHERE
I would go with the Join query as Quassonoi suggested in his answer, If you want to try an alternate solution, you can do it with subquery like this
SELECT P.PostId,P.Title,P.Body
FROM Post P WHERE P.CreatedById
IN (
SELECT FollowerID from Followers WHERE USER_ID=3
)
Replace 3 with the current user id. Assuming your table structure is something like this.
POST
PostId (int)
Title
Body
Followers
UserId (int)
FollowerId (int)

Categories