Consider this query:
SELECT table1.id,
table1.review,
table1.time,
table2.author,
table2.title
FROM
table1, table2
WHERE table1.id = table2.id
AND table1.reviewer = '{$username}'
ORDER BY table1.id
I'm using the above quite a lot around my site's code. I find that adding the table prefixes etc. before the column names can make the query very long and take up quite a lot of lines.
Is there a way to make the above query simpler/easier?
First of all, you may want to give shorter aliases to your tables. In addition, you are using the implicit join syntax which complicates the WHERE clause, and is not recommended in general. You may want to use the more modern explicit syntax instead:
SELECT t1.id, t1.review, t1.time, t2.author, t2.title
FROM table1 AS t1
JOIN table2 AS t2 ON (t2.id = t1.id)
WHERE t1.reviewer = '{$username}'
ORDER BY t1.id
Note that JOIN is a synonym for INNER JOIN, and the AS keyword is optional when defining table aliases. You can simply use ... FROM table1 t1 ... instead of ... FROM table1 AS t1 ....
You can use table aliases
SELECT t1.id, t1.review, t1.time, t2.author, t2.title
FROM
table1 AS t1, table2 AS t2
WHERE t1.id = t2.id AND t1.reviewer = '{$username}'
ORDER BY t1.id
Why dont you make a function , pass the table name and other parameter and return either values or sql query.
Your query is already simplified, i think you are worried about doing this multiple times. so better will be to create a small function in that case.
I had similar problem, which i got rid using function.
You query looks pretty optimal aside from the potential risks with your "{$username}" portion. I could very easily see some SQL injection issues if the overall query is being rendered as a straight string and not via an abstract layer of some sort.
The format I find most readable is:
SELECT
t1.id,
t1.review,
t1.time,
t2.author,
t2.title
FROM
table1 t1
JOIN table2 t2
on ( t2.id = t1.id )
WHERE
t1.reviewer = '{$username}'
ORDER BY
t1.id
It's not clear if you're "using the above code all over the site" verbatim, or the style of SQL without aliases.
If this specific piece of code is being copy/pasted, consider encapsulating this logic in a function in your PHP modules.
function GetReviewerTitles($reviewer)
{
//your select statement.
$reviewerSQL = sprintf("SELECT t1.id, t1.review, t1.time, t2.author, t2.title FROM table1 as t1 INNER JOIN table2 AS t2 ON t1.id=t2.id WHERE t1.reviewer = '%s' ORDER BY t1.id",
mysql_real_escape_string({$username}));
// Perform Query
$result = mysql_query($reviewerSQL);
//return if/when necessary
}
I suggest using a view:
CREATE VIEW ReviewInfo(id, review, time, author, title, reviewer) AS
SELECT t1.id, t1.review, t1.time,
t2.author, t2.title, t1.reviewer
FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id
Now you can write your query as:
SELECT id, review, time, author, title
FROM ReviewInfo
WHERE reviewer = '{$username}'
ORDER BY id;
Note the use of abbreviations for the table names in the view definition - that is a technique you could use in your query even if you don't use a view. And the notation using an explicit JOIN operator in the FROM clause with the ON condition is preferred to the old-style FROM table1, table2.
Related
Just want to know can I make a such query?
SELECT
t1.id,
t1.name
FROM t1
LEFT JOIN t2 ON t2.id = (SELECT id FROM t3 WHERE t1.address LIKE 'street%' ORDER BY name ASC)
ORDER BY name DESC
This is because I want to add more complex query to the LEFT JOIN with sorting and order and some statements which depends on another table..
Thanks!
U.P.D.
SELECT * FROM t3 WHERE t1.address LIKE CONCAT(address,'%') ORDER BY LENGTH(address) DESC
Actually, I want to write this query as LEFT JOIN subquery (ORDER BY in case of LIKE does make sense!).
Theoretically you can do this.
Writing subquery in join statement will have no effect other than filtering the cartesian product of the two tables just like the where condition. But writing query this way makes no sense as we don't know the context in which you are using it.
The above query can be written in much cleaner way as follows :
SELECT
t1.id,
t1.name
FROM t1, t2
WHERE t2.id in (SELECT id FROM t3 WHERE address LIKE 'street%')
ORDER BY name DESC
It will produce the same result set as the query you provided
I'm posting this question because I did manage to find a similar question, but they weren't using aliases.
I have two tables - I want to grab everything from table1 and just grab the user_name and Team from table2.
My original query is grabbing everything from table2
SELECT *
FROM qabin.allqas t1
JOIN login.users t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
That's all well and good and works fine, but I would like to just get the user_name and the Team
To make things more interesting, they are in different databases, though it hasn't been an issue.
One is in the qabin database and the other is in the login database.
I have tried:
SELECT
qabin.allqas.* AS t1,
login.users.Team,
login.users.user_name
JOIN login.users t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
I need the t1 and t2 aliases because they are used elsewhere for building a larger query string.
Thanks in advance!
Why not use a subquery?
SELECT *
FROM qabin.allqas t1
JOIN (SELECT user_name, Team FROM login.users) t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
You cant use one alias (t1) for a set of columns. I guess what you are looking for is something like:
SELECT t1.*, t2.team, t2.user_name
FROM qabin.allqas t1
JOIN login.users t2
ON (t1.Submitter = t2.user_name)
WHERE t1.Status='Complete'
I would recommend using the real column names from t1 instead of t1.*
Try This query :
SELECT t1.*, t2.user_name, t2.Team
FROM qabin.allqas t1
JOIN login.users t2
ON t1.Submitter 1= t2.user_name
WHERE t1.Status='Complete'
I'm just getting the hang of JOINs in SQL (very powerful, could have made my code a lot more efficient if I'd looked them up earlier!), but am struggling with joining two or more tables with the same column name, then processing with PHP.
Here's the query that I've been trying, using aliases
SELECT *, TABLE1.ID AS t1_id, TABLE3.ID AS t3_id
FROM TABLE1, TABLE2, TABLE3
etc (with a left join)
Only table1 and table3 have the same ID column name, is there something wrong in this code? I'm getting the dreaded mysqli_error() in PHP!
Any help greatly appreciated - can't seem to find the solution elsewhere when selecting everything from more than one table. Could specify each column name, but there would be over one hundred!
SELECT
t1.ID AS t1_id,
t2.ID AS t2_id,
t3.ID AS t3_id
FROM
TABLE1 as t1
LEFT JOIN TABLE2 AS t2
LEFT JOIN TABLE3 AS t3
i have select:
SELECT t1.*, t2.* from t1 left join t2 on t2.id = t1.id
when id exist or not exist in t2 I want all columns from t2 except t2.id (which can be NULL).
Is there any way do this without print out all columns names in query?
thx
No, you have to either specify all columns of interest or use a wildcard.
SELECT *
FROM t1
LEFT JOIN t2
USING (id);
That is, by the way, the reason, why it is a good idea to name the columns that you are going to use in joins the same way across the database.
If the two tables have only id as a common column, you can also use this:
SELECT *
FROM t1
NATURAL LEFT JOIN
t2
Just switch the order of requested tables
instead
SELECT t1.*, t2.* from t1 left join t2 on t2.id = t1.id
use
SELECT t2.*, t1.* from t1 left join t2 on t2.id = t1.id
The Problem Occur With "ID" Now you can do like this:
"SELECT task.*, users.emp_id,users.emp_name,users.emp_designation from task
LEFT JOIN users ON task.userid=users.id where users.sup_id='1021' AND task.date='2022-09-03'"
here i will get the task table id and all data & users table 3 column . easy and simple
Guys, I have two basic tables (id, first_name).
I want to run table 1 against table 2, delete duplicates, and spit out the cleansed version of table 1.
What's the easiest way to do this with PHP/MySQL?
Thanks.
This will delete all records from t1 that also exist in t2, leaving you with a stripped-down t1, but possibly with records in t2 that don't exist in t1:
DELETE FROM table1 t1
WHERE t1.id IN
(SELECT id from table2 t2
WHERE t2.id = t1.id AND t2.first_name = t1.first_name
)
*You may want to consider using EXISTS instead of IN, as per Brian Hooper's suggestion.
This will combine the two tables into a third table (t3), removing duplicates along the way:
SELECT * INTO t3
FROM t1 UNION SELECT * FROM t2
That will work for SQL Server (definitely) and MySQL (I think) but MySQL supports CREATE TABLE table_name AS select-statement, so you could use:
CREATE TABLE t3 AS
(SELECT * FROM t1 UNION DISTINCT SELECT * FROM t2)
*The DISTINCT keyword is optional - it's the default behaviour
DELETE FROM table1 t1
WHERE EXISTS (SELECT *
FROM table2 t2
WHERE t2.id = t1.id AND
t2.first_name = t1.first_name);
I'd prefer the exists to IN which I have had trouble with in the past, with it taking a long time.
Why not use
DELETE FROM table1 WHERE first_name IN (SELECT first_name from table2)
?
Correct me if I missed something.
This is without using subquery (not tested):
DELETE t1
FROM table1 t1
JOIN table2 t2
ON t2.id = t1.id
AND t2.first_name = t1.first_name