Retrieving MYSQL Join values from PHP - php

I have two or more tables which hold values under the same column name. Now when I join these two tables the column names stay the same. When retrieving these values in PHP ($row['name']) I encounter problems since the column 'name' is used twice.
Are there any possible means of separating these two columns inside the query?
SELECT *
FROM stories s
JOIN authors a ON a.id = s.authorid
Table stories
id, name, content, date
Table authors
id, name, date
When i join these two i get one table with similar 'name' columns.
Is there anyway to separate the two tables so the author table has a prefix in front of it? E.g. authors_name /authors_*

Yes, change your SQL this way :
SELECT
s.Id as StoryId,
s.Name as StoryName,
a.Id as AuthorId,
a.Name as AthorName,
FROM stories s
JOIN authors a ON a.id = s.authorid
Then in php, use StoryId, StoryName, AuthorId and AthorName instead of Id or Name.
Hope it helps you.

Yes there is!
SELECT *, stories.name AS s_name, authors.name AS a_name
FROM stories s
JOIN authors a ON a.id = s.authorid
And there you have it. All fields plus two extra! ;)
Hope it helps.

Sorry you can't really do prefixing on a field level, but if you can call 2 queries, just use
SELECT s.*
FROM stories s
JOIN authors a ON a.id = s.authorid
The result set will only contain stories fields and you can use the fields as you normally would with php. Then just do the same again for authors.

Related

Joining 2 tables in PHP

This might be a dumb and very simple question, but I'm stuck and I've tried multiple ways already. So I have this code:
$RefLinksRAW = $GLOBALS['DATABASE']->query("
SELECT u.id, u.username FROM ".USERS." as u
LEFT JOIN ".TURNAMENT." as s
ON s.id_owner = u.id;");
But I want to also SELECT columns from TURNAMENT that corresponds to s.id_owner, how do I do that?
Basically I want to make a table that shows contents from both of those tables.
This is the TURNAMENT table, I want to make sure that 'id' from USERS table is same as 'id_owner' and also select 'wons' column
You can just add the columns you are after to the SELECT section. For example,
SELECT
u.id,
u.username,
s.somecolumn,
s.someothercolumn
FROM
users u
LEFT JOIN
turnament s ON s.id_owner = u.id;
Replace users and turnament with your actual table names, and somecolumn/someothercolumn with the columns you are after.

Get information from another table using intermediate table

Here I have three tables authors, books_has_authors and books.
Books_has_author is an intermediate table which contains only the primary keys form other two tables. I am trying to write an sql query which will return me all the books name written by a particular author. I am new to relational database concepts as well as writing queries for relational databases. I know some join queries but I'm really confused how to use them for fetching information through an intermediate table. Any help will be appreciated ! :)
You only need to add an additional JOIN, like this:
SELECT b.book_id, b.book_name, a.author_id, a.author_name
FROM books b
JOIN books_has_author ba ON ba.books_book_id = b.book_id
JOIN author a ON ba.author_author_id = a.author_id
WHERE a.author_id = xxx
Try this:
SELECT b.* FROM books b
INNER JOIN books_has_author bha
ON bha.books_book_id = b.book_id
WHERE bha.author_author_id = "$$YOUR AUTHOR ID"
use JOIN like that :-
SELECT *
FROM books
JOIN books_has_author ON books_has_author.books_book_id = books.book_id
JOIN author ON books_has_author.author_author_id = author.author_id
WHERE author.author_id = '123'

How to get data referenced in another table in one MySQL query

I would to select some data from mysql. However, some of the data stored in the table I am querying from are in codes and to get the text description I need to reference that data to another table.
TABLE: persons
SELECT id, first_name, last_name, address_code, customer_type_code
FROM persons
WHERE id = 1001
TABLE: ref_address
SELECT address_name FROM ref_address
WHERE address_code = 123
TABLE: ref_customer_type_code
SELECT customer_type_name FROM ref_customer_type_code
WHERE customer_type_code = 456
How can I combine all three queries together to return id, first_name, last_name, address_name, customer_type_name in one query instead of querying them 3 times like this?
Please read the reference manual for join.
In short, you need to define a relation between your tables (I use aliases just to make things a bit "cheaper" to write):
select p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code
, ra.address_name
, rctc.customer_type_name
from persons as p
-- Join the persons table with the ref_address table,
-- using the address_code column of each table
inner join ref_adress as ra
on p.address_code = ra.address_code
-- Join the persons table with the ref_customer_type_code table
-- using the customer_type_code column of each table
inner join ref_customer_type_code as rctc
on p.customer_type_code = rctc.customer_type_code
where p.id = 1001
Notice that when you use multiple tables in a query it may be useful to define aliases to avoid having to write again and again the full name of the table. Also, it may be a good idea to explicitly specify each field's source table (by alias, if you are using it)
What you're looking for is a JOIN.
In a JOIN, you specify two tables and how they are related to one another. In a single SELECT statement, you can have multiple JOIN clauses.
SELECT
p.id, p.first_name, p.last_name, p.address_code, p.customer_type_code,
a.address_name,
t.customer_type_name
FROM
persons p
JOIN ref_address a
ON p.address_code = a.address_code
JOIN ref_customer_type_code t
ON p.customer_type_code = t.customer_type_code
WHERE
p.id = 1001
This query says that the table persons and ref_address should be linked, or "joined", by the related columns address_code which are available in each table. Same goes with the tables persons and ref_customer_type_code being linked by the columns customer_type_code.

Getting both Id's from inner join

I am working on a website in the php Codeigniter framework, and am having problems with joining tables together.
The two tables are meals and restaurants, simply with the versions
meals: id, restaurant_id, price, etc.
restaurants: id, name, location, etc.
To make the query I use the following code:
$this->db->join('restaurants', 'restaurants.id = meals.restaurant_id');
$query = $this->db->get('meals');
which returns the same result as running this:
$query = $this->db->query('SELECT * FROM meals INNER JOIN restaurants ON restaurants.id = meals.restaurant_id');
The problem is that when I access the result in the form of a php array or and object (as supplied by codeigniter DB class), there is only one id returned, the restaurant id, and I need the meal id. How can I get it to spit out the meal id?
I'm not experienced with your current tools (php/codeigniter/etc.), and I'm assuming the problem comes about because both tables have the same name for the "id" column? (That's not normally a problem... but maybe the tools don't handle it right?) As for the SQL query, one way to differentiate columns with the same name is to use aliases in the select statement. Here is an example:
select
m.id as meal_id,
m.price as meal_price,
r.id as restaurant_id,
r.name as restaurant_name,
r.location as restaurant_location
from meals as m
inner join restaurants as r on r.id = m.restaurant_id
try this
SELECT t1.id as mid,t2.id as rid
FROM table_meals as t1
inner JOIN table_restaurant as t2 ON t2.id = t1.`restaurant_id`

There are two columns in the new table(table1 join table2) which have the same column name, how to get the values of both respectively

select * from table1 join table2 on table1.column3=table2.column4 where ...
...
$row=mysql_fetch_assoc($result);
However, there are two columns in the new table(table1 join table2) which have the same column name, how to get the values of both respectively?
Call the columsn out specifically with an alias like
SELECT table_1.id as table_1_id, table_2.id as table_2_id
Youll have to list out all or most of the columns form at least one of the tables this way but you can get access to cols with the same name across multiple tables.
Prefix the column name in the select with its table name.
select table1.my_column, table2.my_column
from table1, table2
where table1.id = table2.t1_id
But with this method, you would have to read the columns using their returned order indexes, rather than their names. Another answer mentioned using as to name each column, so consider that if you're going to read them by name.
When there's a column name collision due to a query joining 2+ tables, you can not use *. You can use:
SELECT table_1.*,
table_2.*
FROM table_1,
table_2
If that doesn't return the list of columns you want, you will have to explicitly list every column. There's no way around - it's an all or nothing deal.
Table Aliases
...but typing out the full table name every time can be a pain, which is why you can alias tables:
SELECT t1.*,
t2.*
FROM table_1 AS t1,
table_2 t2
Either alias notation is supported. They're also required if you want to join a table to itself.
Using only the column names in a Select list that you actually need\ is always the best, but when you want everything, then, well, go ahead and use the *:
Select a.*,
b.*,
a.id as a_id,
b.id as b_id,
a.name as a_name,
b.name as b_name
from tablea a,
tableb b
...
It won't hurt to be redundant, as a.* includes a_id and a_name, but there values from the * get lost in the associative array, so just put them back in with new, unique names.

Categories