Output from two tables that has two same name columns - php

I do have a MySQL script that put together two tables using INNER JOIN. Both tables, have a field called ID.
I do need to output with PHP, both ID fields. How do I do this?
This is my script:
$sql = "SELECT a.id,
a.route_id,
a.requester,
a.reservation,
a.reservation_date,
a.reservation_by,
a.telephone,
a.email,
a.firstname,
a.lastname,
a.qty_pax,
a.date_trip,
a.time_trip,
a.trip_type,
a.cancelled,
a.notes,
a.room,
a.driver_id,
b.id,
b.dep_symbol,
b.dep_location_id
FROM
general_reservations a
INNER JOIN
routes b
WHERE
a.cancelled<>'2'
AND a.date_trip BETWEEN '$find_begin' AND '$find_end'
AND b.dep_symbol LIKE '$code'
AND b.id LIKE a.route_id
ORDER BY a.date_trip, b.dep_symbol, a.route_id";
Notice that I do have a.id and b.id from two different tables.

Use an alias name,
$sql = "SELECT a.id AS aid,
a.route_id,
a.requester,
a.reservation,
a.reservation_date,
a.reservation_by,
a.telephone,
a.email,
a.firstname,
a.lastname,
a.qty_pax,
a.date_trip,
a.time_trip,
a.trip_type,
a.cancelled,
a.notes,
a.room,
a.driver_id,
b.id AS bid,
b.dep_symbol,
b.dep_location_id
FROM
general_reservations a
INNER JOIN
routes b
WHERE
a.cancelled<>'2'
AND a.date_trip BETWEEN '$find_begin' AND '$find_end'
AND b.dep_symbol LIKE '$code'
AND b.id LIKE a.route_id
ORDER BY a.date_trip, b.dep_symbol, a.route_id";

Related

How to show name of course in INNER JOIN?

I have two tables: users and courses. Inside users table i have filed course where i have course id. Inside courses table i have just ID and NAME.
I need to get popular course. I do request:
SELECT u.course, COUNT(*) as freq FROM users u INNER JOIN courses c ON u.course = c.id GROUP BY u.course
As a result: id => freq. But i need to replace ID to NAME of course. How?
Thanks.
You don't say what database you use, but I would assume you can use CTEs since most modern databases do. Your query can be written as:
with x as (
select course, count(*) as freq from users group by course
),
y as (
select max(freq) as max_freq from x
)
select c.name, x.freq
from x
join y on x.freq = y.max_freq
join courses c on c.id = x.course
This query has the [desirable?] side effect that it shows more than one course, if there are more than one tied in first place.
Add c.name to both the SELECT clause and the GROUP BY clause.
SELECT u.course, c.name, COUNT(*) as freq
FROM users u
INNER JOIN courses c
ON u.course = c.id
GROUP BY u.course, c.name;
Demo: https://dbfiddle.uk/?rdbms=mariadb_10.3&fiddle=02a41e0f1e6407e516e91c49b4bdc1d2
SELECT u.course, COUNT(*) as freq, c.name FROM users u INNER JOIN courses c ON u.course = c.id GROUP BY u.course
If your DBMS supports row_number this will be suitable:
select t.id, c.name, t.cnt
from course c
join (
select c.id, count(1) cnt, ROW_NUMBER() over(order by count(1) desc) rn
from users u
join course c on c.id = u.course
group by id
)t on t.id = c.id and t.rn = 1

retrieving values from multiple tables in mysql

I am using php and I have to get the data from multiple tables with common id, but the problem is that in few tables that common id contains multiple records,using inner join gives me separate rows of data e.g.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"2"}
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId":"3"}
extraId is the multiple record for the dish_id:52.
I need result like this.
{"dish_id":"52","quantity":"1","STATUS":"pending","franchise_id":"5","order_type":"PickUp","extraId"[{"id":"2"},{"id":"3"]}}
My query is:
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status'";
please help.
Use group by and group_concat. Something like this:
Select d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type,
group_concat(e.extra_id) as extraids
from order_main m INNER JOIN
order_detail d
ON m.id = d.order_id INNER JOIN
order_extras e
ON m.id = e.order_id INNER JOIN
order_addons a
ON m.id = a.order_id
where m.franchise_id = '$storeId' and d.STATUS <> '$order_status'
group by d.dish_id, d.quantity, d.STATUS, m.franchise_id, m.order_type;
Your desired results do not include these columns:
e.extra_title
a.addon_id
a.addon_size
I would also suggest that you remove the join to order_addons.
Notice that table aliases make the query easier to write and to read.
You can use group bywith dish_id
$orders = "Select DISTINCT order_detail.dish_id,order_detail.quantity,order_detail.STATUS,
order_main.franchise_id,order_main.order_type,
order_extras.extra_id,order_extras.extra_title,
order_addons.addon_id,order_addons.addon_size
from order_main
INNER JOIN order_detail ON order_main.id=order_detail.order_id
INNER JOIN order_extras ON order_main.id=order_extras.order_id
INNER JOIN order_addons ON order_main.id=order_addons.order_id
WHERE order_main.franchise_id='$storeId'
and
order_detail.STATUS!='$order_status' group by order_detail.dish_id";

PHP SQL Adding Multiple Selects

I am using the Sql query below:
$query = mysql_query("SELECT * FROM `venues`
LEFT JOIN `follows` USING (venue_id)
LEFT JOIN `stats` USING (venue_id, user_id)
WHERE follows.user_id = $userid");
The problem is that it's not showing some fields in the stats table.
So what I would thinking the problem might be (might be wrong), is that I need to select all the fields of that table?
If this is the case, is there a way of telling it to select * the fields for the 3 tables?
For example:
SELECT * FROM `venues`, SELECT * FROM `follows`, SELECT * FROM `stats` LEFT JOIN ....
use alias for each table then
select A., B., C.* from venues as A left join Follows as B.....
You can use alias for tables. Here's your request using those aliases:
$query = mysql_query("SELECT a.*, b.*, c.* FROM `venues` as b
LEFT JOIN `follows` as b USING (venue_id)
LEFT JOIN `stats` as c USING (venue_id, user_id)
WHERE follows.user_id = $userid");

MySQL joining tables with ORDER BY gives unexpected result

I have this database design:
**users_tbl**
id
username
name
**posts_tbl**
id
url
users_id *FK REFERENCE to users table*
**posts_contents_tbl**
id
posts_id *FK REFERENCE to posts table
title
description
date
views
click
isDeleted
I'm using this query
SELECT a.name,a.username,c.*
FROM users_tbl a
LEFT JOIN posts_tbl b ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC
Why I try to run this query it gives me NULL results, sample output is like this
But when I try to remove the ORDER BY c.id ASC it gives me this output:
That's not my expected result.
My expected result would be it will display the posts_contents_tbl in Ascending order at the same time it won't show some null values. Some users in my database doesn't have posts data in the posts_tbl so they should not show too.
How would I do that one? Your help would be greatly appreciated and rewarded!
Thanks!
PS: I already have thousands record in my database.
In that case, you have to use INNER JOIN instead of LEFT JOIN because you only want users with posts to show. The reason why there are Null values is because the records are based on table users_tbl and you've mentioned that some of them have no post. Right?
Try this:
SELECT a.name,
a.username,
c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
INNER JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY c.`date` DESC
I think this is what you are looking for:
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b
ON a.id = b.users_id
LEFT JOIN posts_contents_tbl c
ON b.id = c.posts_id
ORDER BY IFNULL(c.id, 0) ASC;
If you really needs that posts_tbl data should not display if not available.And all data of posts_contents_tbl then you need a RIGHT JOIN and INNER JOIN .
The Query like :-
SELECT a.name,a.username,c.*
FROM users_tbl a
INNER JOIN posts_tbl b ON a.id = b.users_id
RIGHT JOIN posts_contents_tbl c ON b.id = c.posts_id
ORDER BY c.id ASC;

MySQL Inner Join 2 tables

SELECT *
FROM tableA a
INNER JOIN tableB b
ON a.someColumn = b.otherColumn
INNER JOIN tableC c
ON b.anotherColumn = c.nextColumn
What if tableA and tableB has the same name in fields? How can I use:
<?
$name = mysql_fetch...
echo $name['a.title'] . ' ' . $name['b.title'];
?>
So I could get title from tableA and title from tableB. Because now, if use just $name['title'] it returns tableA's title. And the above code unfortunately gives just an empty string.
Instead of doing select *, you can put an alias on the columns as well.
SELECT a.title AS 'a_title', b.title AS 'b_title'
-- ...
Then your PHP would be something like:
$name['a_title']
Use an alias
SELECT a.title AS TitleA,
b.title AS TitleB,
...
FROM ...
Then reference the alias:
$name['TitleA']
Stop using SELECT *!
Explicitly list your columns, which will then allow you to alias them as needed.
SELECT a.title AS TableATitle, b.title AS TableBTitle, ...
Instead of using SELECT *, you will have to call out the fields by name. When you do this, you can assign an alias to be used for each.
SELECT a.title AS aTitle, b.title AS bTitle, etc...
SELECT *, a.title as atitle, b.title as btitle
FROM tableA a
INNER JOIN tableB b
ON a.someColumn = b.otherColumn
INNER JOIN tableC c
ON b.anotherColumn = c.nextColumn

Categories