SQL problems - same field names in multiple MySQL tables? - php

I have a keyword search that searches multiple DB tables for thumbnail images using UNION ALL. I have two pages, results.php, and view.php. My goal is to able to click a thumbnail image on results.php and be directed to a larger version of that same image on view.php. The problem is each image in all my tables uses the field name "id" so when I click a thumbnail on results.php I get two different images with the same id from different tables. I tried changing the id's to different names, but when it was time to pass url parameters I can only choose 1 value. (if you can choose more than 1 I don't know how). So my question is why are my id's from different tables being grouped together, and how can I change this?
Image Results Page (which works perfect):
SELECT *
FROM table1
WHERE keyword LIKE %colname% OR id LIKE %colname%
UNION ALL
SELECT *
FROM table2
WHERE keyword LIKE %colname% OR id LIKE %colname%
View Image Page (having problems here):
SELECT *
FROM table1
WHERE id = colname
UNION ALL
FROM table2
WHERE id = colname

you need to namespace your table field names:
SELECT * FROM table1 WHERE table1.id = colname UNION ALL FROM table2 WHERE table2.id = colname

Add a derived field to both of the subqueries, so you can figure out which table that particular row came from:
SELECT 'table1' AS source, *
FROM table1
...
UNION ALL
SELECT 'table2' AS source, *
FROM table2
then have your code check that new 'source' field so you can point at the appropriate DB table later on when it comes time to display that image.
Of course, if both tables have an identical structure, that begs the question of why you didn't just combine them into a single table and add this 'source' field in this new "global" table, saving the trouble of having to run two queries to fetch image data.

Don't forget you can use the AS keyword to alias fields and tables
SELECT *
FROM table1 as t1
WHERE t1.keyword LIKE %colname% OR t1.id LIKE %colname%
UNION ALL
SELECT *
FROM table2 as t2
WHERE t2.keyword LIKE %colname% OR t2.id LIKE %colname%

SELECT *, 1 as table_reference
FROM table1
WHERE id = colname
UNION ALL
select *,2
FROM table2
WHERE id = colname

Your id's aren't being grouped together by mysql. It's just that you're requesting the database results as associative php arrays, with the column names as the array keys. You can't have duplicate array keys, so you only get one of the values.
If you don't need to know the name of the table, just fetch a numerically indexed array instead of associative array. If you need the table names, either modify your sql to alias the columns, or you can use more automated methods
SQL Select * from multiple tables

1) If all your tables have the same structure,
its a huge design flaw
and you ought to merge them in one table.
2) if your tables are different and you have to show only one image - what's the point in using union on the view page? if it's just one image from one table - why do you query 2 tables?

Related

In MySQL, how do I select * from two different tables?

I have two separate tables on MySQL, and I want to make a query where I select * from both of them and I want to order them by id (or date). The problem is the id's will conflict, since there will be two articles with the same id (one from each table).
Is there a way where I can display all the articles of each table in one single query?
Edit:
They don't have the same number of columns. However, I will be choosing the same columns of each of them. So I'd be selecting "id, map, title, summary, image, date, publish" from both table1 and table2. (because both of the tables have those exact same columns).
Then I want to display on a single web page all the articles from both table1 and table2. In the exact same grid.
Is this even possible or do I need to merge both tables in MySQL?
If the 2 tables have the same number of columns and the data types of the corresponding columns are the same, then you can use UNION ALL:
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
ORDER BY id
If you also want to distinguish the rows so you know from which table they originate, you can create a dummy 1st column for each of them like this:
SELECT '1' fromtable, * FROM table1
UNION ALL
SELECT '2' fromtable, * FROM table2
ORDER BY id

How to change column name while joining the query in mysql

I need to change the column name while joining the MySQL query. I am explaining my code below.
select * from grc_action left join grc_users on grc_action.action_owner=grc_users.user_id
I am giving my table below.
grc_action:
id name action_owner
grc_users:
user_id name
The above is my table structure and as both has same column name i.e-name here I need to change the grc_users table column i.e-name while fetching the record. Please help me to solve this problem.
You can use AS
SELECT table1.name AS exampleName, table2.name AS otherName FROM sometable
You can also use this on tables:
SELECT vltn.id, vltn.name FROM veryLongTableName AS vltn
You dont need to type the AS, you can just do SELECT table1.name exampleName to shorten it, but it increases readbility and maintanability to write it, so I recommend doing it with AS.
You may assign different aliases to the name columns from the two different tables.
select
ga.id,
ga.name as action_name,
ga.action_owner,
gu.user_id,
gu.name as user_name
from grc_action ga
left join grc_users gu
on ga.action_owner = gu.user_id;
Note that I also used table aliases which make the query easier to read. In general, doing SELECT * is undesirable, and it is usually better to explicitly list the columns you want.
select *
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
changed query use this query
select *,grc_users.name as grcuser_name,grc_action.name as grcaction_name
from grc_action
left join grc_users on grc_action.action_owner=grc_users.user_id
Here geting two name like this
grcuser_name ,
grcaction_name

How to get value from one table column when two columns of the same name exist in an sql join

I am currently faced with the problem of trying to get a value when I have joined two tables with columns of the same name eg: table1.date and table2.date (date is different in each table) how would I get the "date" of table 1 in this instance.
I am currently running
while($row = $mysqliquery->fetch_object()) {
is there any way that I could use some form of syntax to retrieve the date from table 1 within the following code
$row->date eg $row->table1.date or something
If not how else would I be able to accomplish this?
Thanks.
You should differentiate between 2 columns with the same name by using an alias for one or both of the 2 columns in the query like this
SELECT a.`date`, b.`date` as b_date
FROM table1 a
JOIN table2 b ON a.id = b.a_id
WHERE some specific criteria
Now when your retrieve the ROW each date has its own unique name i.e.
$row->date;
$row->b_date;
I was thinking more along the lines of this ( ficticious ) pseudo sql
select a.`account_id`, a.`date` as 'account_date', u.`date` as 'signup_date'
from `accounts` a
left outer join `users` u on u.`uid`=a.`uid`
Each table has a column called date but in the sql they are each referenced with a unique alias. You cannot have two columns in the same query with the same name or you'll get errors, so you give them an alias. The tables are also aliased in this code - it really helps simplify things in my opinion... Anyway, just a quick example to illustrate how you might approach this - others may have a different approach from which I might learn new tricks

Get MySQL table.column named records with PHP

For example:
I have the normal column name:
SELECT column FROM....
The way i retrieve it in PHP:
$var = $row["column"];
What i want to know is how to retrieve column records which are named with column and table:
SELECT table.column FROM...
It works the exact same way, it's not going to be prefixed with a table.
so if you do a SELECT table.* FROM ..., and one of the column names is tacos. you can still do $row['tacos'].
Now, if you're doing multiple tables (joins) it can get ambiguous if they have the same names, such as "id". So you can alias them... SELECT table.column as new_name FROM ...
You won't get them returned with table.column. Perhaps use an alias? Could use a delimiter between table / field name part.
SELECT column AS tableName_ColumnName FROM....
It sounds to me like you're trying to select from multiple tables which can be done by using joins:
Lets say you have 3 tables that you need to obtain information from all by a unique ID. You can accomplish this by using a left join:
SELECT a.uniqid, a.testingDate, a.testscore, a.testresults,b.email_addy, b.phone1, c.testlevel as tlevel
FROM Tbl1 a
LEFT JOIN Tbl2 b
ON a.Tbl1 = b.Tbl2
LEFT JOIN Tbl3 c
on a.Tbl1 = c.Tbl3
you can then pull the information just as you always do: $var = $row['testingdate']

Msql Inner Join Query Dilemma - Need Joined Auto Increment Value

I have two mysql tables that I am querying together and the query works fine. The tables are car and requests
The problem is that i need both the auto increment id's and it only gives me one.
The one it gives me is not the joined tabled.
Here is my query so far
SELECT * FROM `car` INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id` WHERE `car`.`user_id` =21
I just need to someway get the auto increment id of the requests table.
As always stack exchange is the best place to come for answers so thanks in advance!
Just specify your query as this and you'll get both but with different names:
SELECT `car`.id AS car_id, `requests`.id AS request_id, *
FROM `car`
INNER JOIN `requests` ON `car`.`make_id` = `requests`.`make_id`
WHERE `car`.`user_id` =21
Note that you will still have an ID column which SHOULD be the requests.id, just diregard it and use car_id and request_id...
When you have to join tables and table contains same name than it creates an ambiguous situation. So always use table name or table alias with column name Like this
SELECT t.column
FROM table as t
OR
SELECT table.column
FROM table

Categories