MySQL using * along with column names from joined tables - mysql

I want to select all the fields from one table and also some specific fields from other tables using LEFT JOIN. Can I use the * to select all the columns from the one table and also name the specific fields from the JOIN?
Something like this:
SELECT * , table2.a, table2.b
FROM table1
LEFT JOIN table2 ON table2.a = table1.a
WHERE ...

To get all the fields from table1 and only certain columns from table2 you would use:
SELECT table1.*, table2.column, table2.column
FROM table1
LEFT JOIN table2 ON table2.a = table1.a
WHERE ...

Syntax wise, it is correct and works. However, it is poor practice to use *. I would recommend you always specify a column list. If the columns change, your application may break. It's also always better for readability.

You specify all columns from one table with table1.* If you use * you will get all columns from all tables.
MySQL is fussy about the order of wildcarded column names - they have to be first.

Related

empty result when using a subquery select

I have two tables (table1, table2) in a database (both with type InnoDB). They both have a column "article". In table1 "article" is the primary index, in table2 "article" is defined as "unique". Both of those columns have data type varchar(32), also the same collation.
I am trying to get a list of all "article" values which are in table1, but NOT in table2.
table1 contains about 5000 rows, table2 contains about 3000 rows, so I should get at least 2000 "article" values as a result. My query looks like this:
SELECT article FROM table1
WHERE article NOT IN
(SELECT article FROM table2);
But this returns an empty result...
When I do it the other way around (i.e. select all "article"s from table2 which are not in table1), it works, that query returns around 700 values.
I suppose this must have to do with the different index/unique status of "article" in the two tables. But how can I modify the query to get it working?
Use a left join instead. It is faster with many values anyway:
SELECT t1.article
FROM table1 t1
LEFT JOIN table2 t2 ON t1.article = t2.article
WHERE t2.article IS NULL
I just found a second solution myself (despite the accepted answer fully working): Apparently in this situation the subquery requires a WHERE clause for the whole query to work. So I added a WHERE clause that will apply to all rows in table2 (i.e. WHERE article != ""). So the complete (working) query now looks like this:
SELECT article FROM table1
WHERE article NOT IN
(SELECT article FROM table2 WHERE article != "");

sql, multiple instances of the same table in FROM

Let's say I have a table named "mytable01". When I want to have two instances of the table in the query I do this:
SELECT t1.column, t2.column
FROM mytable01 t1, mytable01 t2;
Now, there are times that I want two instances from the same nested SELECT. Is there a way to do it without having to write it two times?
No! You want to do a self-join (joining the table with itself) you must to declare it each time. By the way try to avoid write queries in implicit format and use explicit join pattern like it:
select t1.column, t2.column
from table t1
join table t2 on t2.id = t1.parentid
btw again, you example missed the columns for the join and that ill force a cross join.

Are there any differences in the following sql statements?

SELECT * FROM table t
SELECT t.* FROM table t
I tried it and it yielded the same results, but I want to make sure because I'm refactoring a piece of code that uses the second version, and I was surprised as it is both longer to write, and less simple.
Are there any hidden stuff here?
MySQL version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)
Both statements are the same in your case.
They would be not if you join multiple tables in one query.
select *
selects all columns.
select t.*
select all columns of table t (or the table assigned the alias t)
SELECT * FROM table t and SELECT t.* FROM table t
Return the whole table
SELECT t.* FROM table as t inner join table2 as t2
will only return the fields in the "table" table while
SELECT * FROM table as t inner join table2 as t2
will return the fields of table and table2
Both the statements will give same results until it's combined with another table with some table operator as Join, Apply where you will need to uniquely identify columns( more specifically ambiguous columns ) from this table.
As a best practice you should use column names instead of using select * as it makes code more readable and front end code doesn't break in case table structure gets changed at any point of time.
The statements are identical. All you have is an alias for table "table" called "t".
SELECT * will return all columns from all tables in the query. SELECT t.* will return all columns from the table named, or aliased as, t. The same in your example because there's only one table involved.

How to access a column result in a left join spanning several tables, with the column appearing twice

I have a left join on 4 different tables and I am fetching the mysql result. A problem I have is that the same column name appears in 2 of the tables and has different values (the join is not performed on this column name).
So in the row result it appears twice. When I go $my_result_object->'desired_column' it accesses the second value of the column. How do I access the first one?
Thanks
you need to add ALIAS
SELECT *, table1.ID AS Table1ID,
table2.ID AS Table2ID, ....
Use aliases:
SELECT mycolumn AS 'column_which_i_need'
You cannot directly add alias when using *, the only way is either to list all the columns, or attach the needed one at the end:
SELECT *, mycolumn AS 'column_which_i_need'

mySQL UPDATE all columns from corresponding columns in another table

I have two tables with identical schema. And lots of columns!
I can update a record from the corresponding table by doing
update t1
join t2 on t2.id=t1.id
set t1.column1=t2.column1,
t1.column2=t2.column2...
where t2.columnx > 123;
But I have a ton of fields and am by nature, a lazy bastard who'd rather shave a yak and post on SE than type out a list of columns (possibly missing 1).
Other than some funky solution that writes out the column list to a text file etc, is there valid mySQL syntax that skips the explicit listing of all the columns and works more like INSERT...SELECT?
REPLACE INTO t1 SELECT * FROM t2 WHERE columnx>123;
Try to use REPLACE INTO ... SELECT ... syntax http://dev.mysql.com/doc/refman/5.0/en/replace.html
REPLACE INTO t1 SELECT t2.* FROM t2, t1 WHERE t1.columnx>123 AND t1.id=t2.id;
Warning! Not tested!
This code would work only if tables have unique indexes on id columns.