Quite simply, I need an MySQL statement that selects all the rows from 2 different tables where userId = 1. Here's the statement I have but it doesn't work. It returns results from table 1 (The left table):
$transactionsQ = $db->query("select e.sender, e.amount, e.timestamp, s.sender, s.recipient, s.amount, s.timestamp from ethTransactions as e, sofTransactions as s where e.userId = ? or s.userId = ?", [$currentUser->id, $currentUser->id]);
table1 (ethTransactions) is like:
|id|userId|sender|amount|timestamp|
table2 (sofTransactions) is like:
|id|userId|sender|recipient|amount|timestamp|
some data on ethTransactions:
1|12|ajdoaidjoiwjdaq|0.03222|2017-11-08 03:09:11
some data on sofTransactions:
1|12|opiyptropyirpyi|dqwuebhdnwq|1.02223|2017-10-08 04:09:11
What I want to return since both have the same userId:
|id|userId|sender |recipient |amount |timestamp |
|1 |12 |ajdoaidjoiwjdaq| |0.03222|2017-11-08 03:09:11|
|1 |12 |opiyptropyirpyi|dqwuebhdnwq|1.02223|2017-10-08 04:09:11|
You should use LEFT JOIN to do that.
SELECT
e.sender as e_sender,
e.amount as e_amount,
e.timestamp as e_timestamp,
s.sender as s_sender,
s.recipient as s_recipient,
s.amount as s_amount,
s.timestamp as as_timestamp
FROM
ethTransactions e
LEFT JOIN
sofTransactions s ON s.userId = e.userId
WHERE
e.userId = ?
SELECT column_names(s) FROM table1 AS t1 **INNER JOIN** table2 AS t2
ON t1.userId = t2.userId WHERE t1.userId=1;
There is something strange about the structure of your database. A JOIN on userid seems natural, but it potentially joins rows from one table with rows from the other table that have different sender columns. This would imply that you are joining data items that do not belong together.
My answer to your question is: redesign your table structure because you are going to break one of the main rules of relational databases - most likely, duplicating data.
Related
Please find picture to get more clarification on what I am asking
Click here
Let me tell you full description
I have two tables -
Table1 contains book_id and book_name
second table -
Table2 contains book_id, stock and cs_id
I want when someone choose cs_id so result displayed in terms of book_name and not book_id
I'm sorry for short description...
I have two tables in mysql database -
table1
SN ID Name
1 E-11 ELC
2 E-13 ELX
3 D-41 DME
and table2
SN ID CS_ID
1 E-11 C01
2 E-13 C01
3 D-41 C54
How to get result of all name from table1 using one cs_id from table2 using php.
Please help.
Thanks in advance!
Here what I'm using
$query = "SELECT table1.id, table2.name FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE table1.cs_id=$cs_id'
this is showing me 0 result.
Use TABLE LEFT JOIN
LIKE
select tb1.name,tb2.ID FROM TABLE1 as tb1 LEFT JOIN TABLE2 as tb2 ON tb1.ID=tb2.ID where tb2.CS_ID='$request_id';
I hope its work
This query performs three JOIN operations with 3 tables. But is not ok i see..i'm trying to output all the rows in echo, but i have bad luck.
Mysql table columns:
tours
------
titlu_slider | desc_slider | poza_slider | poza_articol | pret
tours_review
----------
name | time_added | review_text
tours_overview
------------
descriere | titlu_box1 | desc_box1 | titlu_box2 | desc_box2 | titlu_box3 | desc_box3 | titlu_box4 | desc_box4
Php code:
<?php
$db = mysqli_connect("localhost", "root", "fidodido", "antonytravel");
$q = mysqli_query($db,"SELECT * FROM tours INNER JOIN tours_review INNER JOIN tours_overview WHERE id = ".$_GET['id']."");
while ($row = mysqli_fetch_assoc($q)) {
$titlu_slider=$row['titlu_slider'];
$desc_slider=$row['desc_slider'];
$poza_slider=$row['poza_slider'];
$poza_articol=$row['poza_articol'];
$pret=$row['pret'];
## Review table
$name_review=$row['name'];
$time_added=$row['time_added'];
$review_text=$row['review_text'];
## Overview table
$descriere=$row['descriere'];
$titlu_box1=$row['titlu_box1'];
$desc_box1=$row['desc_box1'];
$titlu_box2=$row['titlu_box2'];
$desc_box2=$row['desc_box2'];
$titlu_box3=$row['titlu_box3'];
$desc_box3=$row['desc_box3'];
$titlu_box4=$row['titlu_box4'];
$desc_box4=$row['desc_box4'];
echo '<section class="parallax_window_in" data-parallax="scroll" data-image-src="'.$poza_slider.'" data-natural-width="1400" data-natural-height="470">
<div id="sub_content_in">
<div id="animate_intro">
<h1>'.$titlu_slider.'</h1>
<p>"'.$desc_slider.'"</p>
</div>
</div>';
Some help needed..thanx.
You need to specify how how the tables relate to each other which might look something like the on conditions shown below (which are just guesses)
SELECT *
FROM tours t
INNER JOIN tours_review trev ON t.id = trev.tour_id
INNER JOIN tours_overview tovr ON = t.id = tovr.tour_id
WHERE t.id = $whatever
You then face the issue of what type of join because if you have a tour with no reviews then you probably still want to list it. For that type of relationship you need an "outer join".
SELECT *
FROM tours t
LEFT OUTER JOIN tours_review trev ON t.id = trev.tour_id
INNER JOIN tours_overview tovr ON = t.id = tovr.tour_id
WHERE t.id = $whatever
If every every tour has an "overview" then that can remain an "inner join"
EDIT: Please note that you need to prefix EVERY column reference with a table name or table alias (I have used table aliases to make the query shorter). If you don't do this your query may fail, e.g. if every table has a column id and you just ask for where id = 123 the query will not know which table to use and the query would error.
INNER join shows the records if there are matching record. Use OUTER join to show all records if it does not exists on other tables.
You are missing a few things in your query. Specifically related to the fields that link the tables. To do these joins the best practice is to name each table and then use that name to in an ON statement to JOIN the tables
So
SELECT * FROM tours
INNER JOIN tours_review
INNER JOIN tours_overview
WHERE id = ".$_GET['id'].""
Should be:
SELECT * FROM tours AS t
INNER JOIN tours_review AS r ON r.somefield = t.somefield
INNER JOIN tours_overview AS o ON o.somefield = t.somefield
WHERE id = ".$_GET['id'].""
MySQL can't join tables if it doesn't know what is connecting them.
I have a table with two fields that reference the ID of another table. I need to pull the name from the other table for both fields.
eg.
Table1
worker1 = 2 (2 is key to other table)
worker2 = 4
Table2
ID NAME
1 Bill
2 Fred
3 John
4 Paul
I need to get $worker1name = Fred and $worker2name = Paul.
So I will be saying something like:
SELECT xxx, NAME?, NAME? FROM Table1
LEFT JOIN Table2 AS p1 ON Table1.worker1 = Table2.ID
LEFT JOIN Table2 AS p2 ON Table1.worker2 = Table2.ID
WHERE ...
$table = mysql_query(...);
$rec = mysql_fetch_assoc($table);
$worker1name = $rec['???'];
$worker2name = $rec['???'];
What do I insert in those last two statements to get the two names. Or more precisely what do I have to add to the SELECT to specify what I want the two different versions of the NAME field from table 2 to be called please?
You should alias the fields also like this:
SELECT
xxx,
p1.NAME as p1name,
p2.NAME as p2name
FROM Table1
LEFT JOIN Table2 AS p1 ON Table1.worker1 = p1.ID
LEFT JOIN Table2 AS p2 ON Table1.worker2 = p2.ID
WHERE ...
Help me out with this query:
I have 3 tables with this structure.
items_to_groups (item_id | group_id)
item_to_regions (item_id | region_id)
items [a bunch of columns]
I need to select every row on the item table that has an item_id match on item_to_groups table WHERE group = x AND has an item_id match on item_to_regions table WHERE region = y
Currently the code I have is a horrible subquery with loops and all.
What would be a better way of doing this?
I've thought about JOIN and such, but can't really get my head around on how to do it.
SELECT bunch_of_columns
FROM items i
INNER JOIN items_to_groups ig ON i.id=ig.item_id
INNER JOIN items_to_regions ir on i.id=ir.item_d
WHERE ir.region_id=y
AND ig.group_id=x
Have a look at the JOIN documentation on MySQL. Joins are important for relational databases.
As you said you have a hard time grasping joins, have a look at A Visual Explanation of SQL Joins by Jeff Atwood. Maybe it helps.
SELECT colums
FROM items
INNER JOIN items_to_groups ON items.item_id = items_to_groups.item_id AND group_id = x
INNER JOIN items_to_regions ON items.item_id = items_to_regions.item_id AND region_id = y
SELECT * FROM items
JOIN items_to_groups ON (items.item_id = items_to_groups.item_id AND group_id = ?)
JOIN items_to_regions ON (items.item_id = items_to_regions.item_id AND region_id = ?)
GROUP BY items.item_id
I have to three table I need to gather data from in a search process:
Commissions Table - Table1: [affiliate_id]
Affiliates Table - Table2: [id][user_id]
Profiles Table - Table3: [ID][NickName]
The search input I'll have is someone searched for a username. I need to return the data from table 1 where the affiliate_id matches the user_id of Table2, that is like the nickname that will be searched for.
I hope that makes sense :)
Try this:
"select table1.* from table1
inner join table2 on table2.user_id = table1.affiliate_id
inner join table3 on table3.id = table2.user_id
where table3.nickname like '%".mysql_real_escape_string($searchtext)."%'"
SELECT t1.*, t3.nickname FROM Table1 t1
JOIN Table2 t2 ON t2.id=t1.affiliate_id
JOIN Table3 t3 ON t2.user_id=t3.user_id
WHERE t2.user_id=?;