I have several table with similar column name in my database.
Let say TABLE 1, TABLE 2, TABLE 3 have a column named : "name". Those are not indexed columns. They contain different and independant data.
If I run a query joining these 3 tables and fetchAll() the results, there will be only one "name" entry in the resulting array since fetchAll() overwrite the column with the same name.
The regular MySQL Library let you access such results doing:
mysql_result($result,0,'TABLE1.name');
How can I let PDO append the table name before the column name like : TABLE1.name, TABLE2.name, etc.
I want to AVOID using alias in my SQL query since I have tons of tables. The only answers I found online uses alias. Is there any parameters we can feed to PDO to do what I am looking for?
You can use aliases instead of direct column names in your SELECT statement. Something like this:
SELECT
table1.name AS t1_name,
table2.name AS t2_name,
table3.name AS t3_name
-- rest of your query
Related
I am making a SQL teaching program. The students will become their Database and Questions. I want to make it possible for them to write any query they want to and give them the result of it.
So it is possible that they write a query like:
SELECT Person.*, Student.ID
FROM Person JOIN Student JOIN Address
WHERE Address.housenr > 20
This makes it hard to use the
SELECT table_name, column_name
FROM INFORMATION_SCHEMA.COLUMNS;
to get the column names. It would be very nice to get those names right from the results of the query. Is it possible to do that in PHP?
You can use the fetch_fields() method to get any information about the columns in your result set. When your query returns at least one row you can use the normal fetch_assoc() method and read the keys from the returned array via array_keys().
Something similar has been asked before but not answered and it's not 100% the same, what i have is an SQL like:
SELECT * FROM table1 JOIN table2;
I know that I can get the columns of a table using DESC, DESCRIBE or SHOW BUT, that function doesn't allow multiple tables neither SQL queries.
Both mysqli::fetch_assoc() and PDOStatement::fetch() can return a row as an associative array, which means that the columns are indexed by column name rather than by number. You can then use the array_keys() function to extract a list of the column names, in order, from the row.
I have two tables:
staticip has two columns: ip, staticipid
rm_users has many columns and one of it is staticipcpe
I would like to find rows in staticip table that is staticipid='2' and ip is not equal staticipcpe in table rm_users.
I tried the following sql statement but it didn't work.
$sqls="select s.ip, s.staticipid from staticip s
where s.staticipid='2' and not exists
(select u.staticipcpe from rm_users u
where u.staticipcpe=s.ip";
I have the following message
"Warning: mysql_result(): supplied argument is not a valid MySQL result resource"
Among other possible problems, you're missing a closing parenthesis:
$sqls="select s.ip, s.staticipid from staticip s
where s.staticipid='2' and not exists
(select u.staticipcpe from rm_users u
where u.staticipcpe=s.ip)";
Also, the mysql_* functions are deprecated. Use MySQLi or PDO.
if you query is right you can use NOT IN Clause to achieve your goal
like
Select Id
From Test
Where Id Not In( Select Foo From Bar )
see for more info
MySQL "NOT IN" query
Depending on your database a JOIN may be better over a sub-query (see Join vs. sub-query), and I also favor backticks (Importance of backtick around table name in MySQL query):
SELECT `s`.`ip`,`s`.`staticipid`
FROM `staticip` AS `s`
INNER JOIN `rm_users` AS `u`
ON `u`.`staticipcpe`<>`s`.`ip`
WHERE `s`.`staticipid`='2';
In plain English:
SELECT: grab the fields "ip" and "staticipid" from the result
FROM: the primary table will be "staticip" - alias as "s"
INNER JOIN: compare the rows in "staticip" with the rows in "rm_users"
ON (part of INNER JOIN): fetch the rows where "staticipcpe" from "ip" is not listed under the field "staticipcpe" from "rm_users"
WHERE: filter results such that the value of "staticipid" must be "2"
Don't forget to consider indexing the fields rm_users.staticipcpe, staticip.staticipid (I assume this one is already a PRIMARY KEY), and staticip.ip, otherwise you're looking at full table scans, rather than taking advantage of MySQL's b-tree lookup from memory.
I would like to select all matches from a commaseparated column in table2, where column could be like this: 0,1,2 OR 2,4,5 OR 2,5 OR 1,3,5 etc.
I have tried with:
SELECT * from table where 1,3,5 IN(SELECT commaseparated FROM table2) WHERE ..
But error on statement when using commas.
I've also tried using REGEXP but in my case i need to search for all matches within 1,3,5
How can i solve this one? :)
Can't do that in standard SQL. it's
WHERE singlevalue IN (list, of, values)
if you want to compare lists against lists, you should revamp your tables so they're properly normalized. Storing formatted data in a field basically negates the purpose of having a relational database - you can't establish relationships with the data if it's not in a format that allows relationships to be formed.
If those CSV lists were in sub-tables, you could do a very simple JOIN query to meet your specifications.
If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;