Select query from mysql database - php

Hello I am having a hard time trying to SELECT all of the information I need from two tables, the following two tables are:
Person: |id|fname|mname|lname| and Related: |id1|id2|relationship|
and I want the following to be displayed back from the SELECT query:
|id1|fname(of id1)|id2|fname(of id2)|relationship|
SO the Related table has two id's that are FOREIGN KEYS to Person(id), and I need to
SELECT id1, (id1's first name), id2, (id2's firstname), and the relationship.
I've tried something like this and a few other SELECT queries but I can't seem to get it to work:
SELECT p.fname, r.id1, r.id2, r.relationship
FROM Person p, Related r
INNER JOIN Related ON first.id = r.id1
INNER JOIN Related ON second.id = r.id2;
Any help will be greatly appreciated! Thank you!

You're joining on Related thrice. You only need to join once, and you need to join on Person again.
SELECT id1, p1.fname, id2, p2.fname, relationship
FROM Person p1
JOIN Related ON (p1.id = id1)
JOIN Person p2 ON (id2 = p2.id)

I've found a website for you (w3schools) and it should have all the information you need for the SELECT function you're trying to get. Hope this helps:
http://www.w3schools.com/php/php_mysql_select.asp

Related

Is it possible with MySQL to generate a query to fetch from several tables even if one has no results?

I'm facing a problem here:
I'm building a forum, this forum has several tables and I'm trying to fetch the comments and user info in a single query.
So far, it should be easy, the problem is that I can't change the structure and with the following query I get a perfect result IF there is a like to the answer. If no one likes the answer it fails.
Select
mfr.mfr_forum_answers.id,
mfr.mfr_forum_answers.date_created,
mfr.mfr_forum_answers.last_updated,
mfr.mfr_forum_answers.content,
mfr.mfr_forum_answers.accepted,
mfr.mfr_forum_answers.user_id,
mfr.mfr_users.level,
mfr.mfr_users.avatar,
mfr.mfr_forum_likes.subject_id,
mfr.wp_users.ID As ID1,
mfr.mfr_forum_topics.user_id As owner_id,
(SELECT count(mfr.mfr_forum_likes.id) FROM mfr.mfr_forum_likes WHERE mfr.mfr_forum_likes.subject_id = :id AND mfr.mfr_forum_likes.type = 'answer') as likes,
(SELECT count(mfr.mfr_forum_likes.id) FROM mfr.mfr_forum_likes WHERE mfr.mfr_forum_likes.subject_id = :id AND makefitreal.mfr_forum_likes.type = 'answer' AND mfr.mfr_forum_likes.user_id = :sessionId ) as i_like,
mfr.wp_users.user_nicename
From
mfr.mfr_forum_likes Inner Join
mfr.mfr_forum_answers
On mfr.mfr_forum_answers.topic_id =
mfr.mfr_forum_likes.subject_id Inner Join
mfr.mfr_users
On mfr.mfr_forum_answers.user_id = mfr.mfr_users.id
Inner Join
mfr.wp_users
On mfr.mfr_users.id = mfr.wp_users.ID Inner Join
mfr.mfr_forum_topics
On mfr.mfr_forum_answers.topic_id = mfr.mfr_forum_topics.id
Where
mfr.mfr_forum_answers.topic_id = :id
And
mfr.mfr_forum_likes.type = 'answer'
So far as said it returns only if an answer has a like, I'm thinking on adding a add to the user who posts the answer by default but I'm trying to improve my skills by solving new issues.
If someone has a suggestion in how I could overcome the fact that if a table is empty, the query continues I'd be really thankfull.
Thanks in advance-
Pihh
Yes. What you are looking for are called left and right joins. According to the documentation, with a LEFT JOIN you still join two tables as normal but
If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table.
This means that you can try to join two tables, but if a row does not have any results it will still return the results from the first table. The same is true for a RIGHT JOIN only it works the opposite way: it will return results if the tabled being joined to has results, but the original table does not.
It looks like you have 3 tables for 3 relationships: there are answers, a user gives an answer, and an answer might or might not have like. To grab this data, I would suggest starting from your answers table, performing an INNER JOIN on your users table (assuming there are always users), and a LEFT JOIN on your likes table. Here is a simple example:
SELECT *
FROM answers
INNER JOIN users ON users.id = answers.user_id
LEFT JOIN likes ON likes.answer_id = answer.id
WHERE answers.id = :id
AND likes.type = 'answers'
Of course, if for some unknown reason you need to start from your likes table, then you'd have to RIGHT JOIN the other tables. I hope that gives you a good idea of how you'd make your query.

mysql join three tables order by third

I have a query
SELECT jss_products.* from jss_products_tree,jss_products
WHERE stock!=0 AND
jss_products.productID = jss_products_tree.productID
and sectionID=1 order by price
Which returns me a big array with products. Now I need to join 3rd table jss_extrafields_values,
which has productID and content fields, and somehow order the array by the content from 3rd table. Also it might have more than one rows with different content but same product.ID.. any ideas on how to join all of them? I tried a lot of tutorials but none of them helped me.. thanks
Its easier to use join over from table1, table2 as its better to read. And you can just join the 3th table.
SELECT jss_products.* from jss_products_tree
INNER JOIN jss_products
ON jss_products.productID = jss_products_tree.productID
INNER JOIN jss_extrafields_values
on jss_extrafields_values.productId = jss_products.productID
WHERE stock!=0 AND
and sectionID=1
order by jss_extrafields_values.field

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

Mysql join query retrieving profile images of users with ids from other table

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

Php fetch rows from multiple MySQL tables

What I want to do is get the data from two different tables (table1 and table2) where row1 = 'test' in both of the tables
You'll want to use an INNER JOIN here - something along these lines (can't tell you for sure since you didn't give the structure of your tables)...
SELECT * FROM thread t
INNER JOIN post_display pd ON pd.threadid = t.threadid
WHERE t.threadid = 2
ORDER BY t.threadid DESC
Note: SELECT * can be very bad if you're selecting a bunch of fields you're never going to need. Once you have the query working, narrow down your select to the specific fields you're looking to work with.
More info on JOIN syntax for MySQL is available here: http://dev.mysql.com/doc/refman/5.1/en/join.html
I'm not quite sure what you're asking, but if you want to fetch columns from multiple tables at once (and it sounds like you're saying rows when you mean columns) you probably want a JOIN, which is an SQL feature
I am not getting what you are asking about.. but.. i can give u suggestion on you asked question.. u can try this.. have a look
SELECT * FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.t1id
WHERE t1.row1 like 'test' AND t2.row like 'row';

Categories