FETCH_ASSOC and SELECT * FROM two tables with the same column name - mysql

I have lot of MySQL tables with the same column name. So Im looking for PDO or SQL hack for SELECT * FROM more tables - which will return table names in result sets.
Example:
'SELECT * FROM table0, table1';
Where both tables has 'name' column. But FETCH_ASSOC result returns only one 'name' - the last one.
Result:
echo $result["name"];
Wanted result:
echo $result["table0.name"];
echo $result["table1.name"];
...
Note that
I cannot rename DB columns to be unique
I cannot manualy create alias for each columns (lot of tables/columns)
I want name in result set not numbers like FETCH_NUM
Any ideas?
Thanks!

Hack doesn't exist, you have to create aliases.

You said that you don't want to alias all columns because there are too many, but have you considered only aliasing the ones that give you problems?
SELECT
*,
table0.name AS t0name,
table1.name AS t1name
FROM table0 JOIN table1 ON ...

Related

SQL query - I don't know all identifiers

Is there a way, where I can see every parameter or identifier I can query from my database? Not the contents but the "column names"
Something like
SELECT * FROM myDb AS String
To simply get the column names and types of a table.
You could SHOW them.
SHOW COLUMNS FROM myTable;
But if you want to know the column names of your table, and only a bit of data from it (to see what it looks like).
Then use LIMIT to get only a few records.
SELECT *
FROM myTable
LIMIT 3
It's fast and easy.
But you can also just see the columns without data if you use a criteria that's false.
SELECT *
FROM myTable
WHERE 0=1
You can also use:
show create table table_name;
but as "LukStorms" mentioned, the below statement shows you the data in table format and in a pretier way
show columns from table_name;
You can use INFORMATION_SCHEMA.COLUMNS to retrieve all columns name
select column_name from INFORMATION_SCHEMA.COLUMNS where Table_Name='Your_Table'

SELECT query with WHERE returns all rows when column name and value are equal

I am confused by how my SELECT query is behaving. Accidentally I read the header row of a .csv file into my table. This means there is now one row in the table which has the column values in each corresponding column.
But a SELECT like this
select * from `mytablename` where segmentering=`segmentering`;
Returns all the rows in the table.
Why is MySQL ignoring the condition?
it should be 'segmentering' not with back tick(``)
select * from `mytablename` where segmentering='segmentering'
Problem is you use single quote ` object identifier for strings.
instead of it use normal single quote:
select * from `mytablename` where segmentering='segmentering';
I guess the problem is with you are adding back quotes(``) with the value field. Back quote will be used for specifying column or table names. It should not be used with the value.
Try using,
select * from `mytablename` where `segmentering`='segmentering';
OR
select * from `mytablename` where segmentering='segmentering';
Try this
select * from `mytablename` where segmentering='segmentering';
Remove `` and replace '';
I hope it will work for you

Nested select statement in mysql -my code doesn't work

here is my query
SELECT con_serial,column2,column3
FROM
(SELECT con_serial,column2,column3
FROM big_table
WHERE ISNULL(contact1, '')+'#'+ISNULL(contact2, '')+'#'+ISNULL(contact3, '')+'#'+ISNULL(contact4, '')+'#'+ISNULL(contact5, '')
LIKE '%' + '".$conserial."' + '%') AS a
WHERE con_serial
IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."')
at the inner select i wish to get the rows which have this value $conserial in one of their 5 columns(contact1...contact5)
and the outer select to choose the rows from it that their column con_serial is one of the variables ($contact1...$contact5)
can anybody see what's wrong here?
Despite your new formulation, it remains very much unclear.
Nevertheless I'll try to give you an answer, based on what I can guess...
First here is how I'd reformulate your need:
you have some values in PHP variables: one $conserial and five $contact# where # is 1-5
the table structure contains at least these columns: con_serial, column2, column3, and five contact# where # is 1-5
you want to select rows where both (here is the most strange part of your need):
at least one of the contact# columns matches the PHP $conserial value
the con_serial column matches at least one of the PHP $contact# values
That said, note that you don't need to have two nested SELECT: you only want each row to satisfy two conditions, so they can be ANDed in the WHERE clause.
Based on that, your query should be:
$query = "
SELECT con_serial, column2, column3
FROM big_table
WHERE con_serial IN ('$contact1', '$contact2', '$contact3', '$contact4', '$contact5')
AND '$con_serial' IN (contact1, contact2, contact3, contact4, contact5)
";
i guess the sum part in where clause is nut allowed
any way i've solved this with using two IN like this
SELECT con_serial,,column2,column3
FROM(SELECT con_serial,column2,column3
FROM
big_table
WHERE '".$conserial."' IN(contact1,contact2,contact3,contact4,contact5)) a
WHERE con_serial IN('".$contact1."','".$contact2."','".$contact3."','".$contact4."','".$contact5."'
this was wat i wanted Tnx any way ;)

MySQL: Select multiple fields

If SQL query contains all fields what I need it will be very large.
How in SQL query I can select field from F3 to F 100 and from F150 to F200?
SELECT F3 to F100, F150 to F200 FROM database;
It is possible or not???
Tables structure change is not available
You have to :
1- manually select all columns . Or
2- do
Select * from database
And then just fetch the columns you need.
There are no shortcuts for this, you will have to list the fields needed one way or another. If the fields being selected are always the same, you should create a view for that as:
CREATE VIEW SomeView AS
SELECT
F3,
...
F100
FROM
SomeTable
and then select like:
SELECT * FROM SomeView
But again, you will have to list the fields at least once.
SELECT F3 to F100, F150 to F200 FROM database;
this query can not possible..
you must have to specify all the columns name
like select F1,f2,f3 from database;
You can't.
But if it's not possible to modify your table structure to fix the database design issue, you can use an SQL query to generate the MySQL query:
SELECT CONCAT('SELECT ', GROUP_CONCAT(COLUMN_NAME), ' FROM `your table`')
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your schema' and TABLE_NAME = 'your table'
GROUP BY TABLE_NAME
Add a filter in WHERE to select only the desired fields.

MySql Select rows where value IN comma-delimited column

I'm trying do something like this:
SELECT * FROM table WHERE column IN (1,2,3)
but in place of 1,2,3 I want to use a column from another table that contains a comma-delimited list just like "1,2,3" above.
I have tried to do this:
SELECT
GROUP_CONCAT(a.eating_area SEPARATOR ', ')
FROM table_areas a
WHERE a.eating_area_id IN (
SELECT
o.eating_area_ids
FROM table_offers o WHERE o.rid=1
)
however this only returns the value associated with 1, and not 2 or 3. Can this be done or is there another way to do this?
Many thanks
SELECT * FROM table t
WHERE IF(FIND_IN_SET(column,(SELECT "1,2,3" FROM otherTable WHERE 1))>=1,1,0)
-- FIND_IN_SET will return the position.
I don't know if it's the best way to do it but... i think it could work.
Source: Find_in_set