i encountered a funny problem:
I have two table t1 and t2 with several columns. Because two columns in the both tables have the same name, i need to to this:
SELECT t1.foo AS ID, t2.bar as NAME
FROM t1, t2
WHERE t1.foo = t2.foo;
The Answers are headed with t1.ID, t2.NAME
I donĀ“t wont the table names in the headlines.
Funny thing: Using the same query in a view returns the correct / wanted headings: ID...NAME
I want to use those queries in stored procedures where the same behaviour has been observed.
Tried with MySQL 5.5.9
Any idea how to avoid the table names in the headlines?
SELECT ID, NAME
FROM (SELECT t1.foo AS ID, t2.bar as NAME
FROM t1, t2
WHERE t1.foo = t2.foo) AS t;
You can strip the linked tablename from a field with a simple string or int function:
SELECT (t1.foo+0) AS ID, CONCAT(t2.bar) as NAME
FROM t1, t2
WHERE t1.foo = t2.foo;
Related
My scheme looks like:
I have two tables: table1, table2
table1 has one field: table1-name of type string.
table2 has one field: table2-name of type string.
I want to return all rows of table1-name that are NOT substring of any table2-name records. I did:
SELECT DISTINCT `table2-name`.`table2`
FROM `table2`, `table1`
WHERE `table2-name`.`table2` NOT IN (SELECT `table1-name` FROM `table1`)
LIMIT 100;
But this returns all table2-name that are not equal to table1-name. What I need is all table2-namethat are not sub-string of table2-name.
Example:
table1-name:aa.abc.com, bb.com, xyz.com
table2-name: abc.com, aaa.com, xyz.com
The query above will return:
abc.com
aaa.com
What I want to return is:
aaa.com
I do not want abc.com to be returned because it is a sub-string of aa.abc.com.
Can you correct my query?
Use NOT EXISTS for such conditions:
select *
from table1 t1
where not exists
(
select *
from table2 t2
where t2.`table2-name` like concat('%', t1.`table1-name`, '%')
);
BTW: You should avoid names like table2-name where you need quotes in order to use them. Use something like table2_name instead.
If table2 is not very big, I wonder how this would perform:
select t1.*
from table1 t1 cross join
(select group_concat(t2.name separator '|') as names
from table2 t2
) t2
where t1.name not regexp t2.names;
Or, equivalently:
select t1.*
from table1 t1 cross join
(select group_concat(t2.name) as names
from table2 t2
) t2
where find_in_set(t1.name, t2.names) = 0;
This assumes that t2.name does not have special characters (for regexp) or commas (for find_in_set()).
To check if something is a substring of something else, you would need to use either 'LIKE' (as shown below) or possibly 'REGEXP_LIKE'.
SELECT DISTINCT table2.*
FROM table2
WHERE NOT EXISTS (SELECT 1 FROM table1 where table1.table1name LIKE CONCAT('%',table2.table2name,'%') > 0)
LIMIT 100;
On another note
1 - your problem description is not consistent (you confuse table1 and table2 repeatedly), choosing a better name would likely help with that.
2 - as you will see, I've taken the liberty of renaming the columns to drop the '-' character.
Another version that will work:
SELECT DISTINCT table2.*
FROM table2
WHERE table2name NOT IN (SELECT table2.table2name FROM table1 where table1.table1name like CONCAT('%',table2.table2name,'%') > 0)
LIMIT 100;
Please check the SQLFiddle (I also copied Thorsten Kettner's version above in there after renaming tables/columns)
I am trying to execute below query which says
SELECT t1.name from table t1, t2 WHERE t2.data LIKE(CONCAT_WS(',' DISTINCT(t1.name)))
OR
SELECT t1.name from table t1, t2 WHERE t2.data LIKE(GROUP_CONCAT(DISTINCT(t1.name) SEPARATOR ','))
Both ways say
#1111 - Invalid use of group function
Well not totally sure what trying to do, but suspect will need a subquery to join on an aggregate function.
SELECT names
FROM t2
INNER JOIN (SELECT GROUP_CONCAT(DISTINCT name SEPARATOR ',') as names
FROM t1
GROUP BY user_id) t1 USING t2.data = names
That query still doesnt really make sence, but might show roughly how to construct it.
As comments say, really need more context to under WHAT you trying to do.
I have the following query which works just fine:
SELECT lastname, firstname, date, complete
FROM table1
WHERE complete NOT IN (SELECT complete FROM table2)
ORDER BY lastname
I'm being asked to provide information from columns that are in table2 but NOT in table1. Like so:
SELECT t1.lastname, t1.firstname, t1.date, t1.complete, t2.newdata
FROM table1 t1, table2 t2
WHERE t1.complete NOT IN (SELECT t2.complete FROM table2)
ORDER BY lastname
However, either this does not work or it somehow got caught in a loop because I had to kill the process after 2 hours.
Is there a way to include data from the table which is being compared (table2)?
I think you question is a little too broad, what is your requirements for t2.newdata?
What you are trying to do doesn't appear to me like it would ever work. You want to get newData from t2, where t2.complete != t1.complete, so how do you know which values of t2 to use? And how do you know which rows to match them with?
I think and I will await a comment from you to know for sure, is that you want all of that information from table 1, and only the information from table 2 when it matches. This describes an outer join.
Try something like this:
SELECT t1.lastName, t1.firstName, t1.date, t1.complete, t2.newData
FROM table1 t1
LEFT JOIN table2 t2 ON t2.complete = t1.complete
ORDER BY t1.lastName;
As I said, this will select all rows from table1, and will put a value in the newData column wherever the complete field has a matching row in table2. If there is not a matching row, the value is null.
See this SQL Fiddle, as well as the above outer join link for more info.
Try this:
SELECT t1.lastname, t1.firstname, t1.date, t1.complete, t2.newdata
FROM table1 t1
join table2 t2 on t2.id=t1.id and t1.complete !=t2.complete
ORDER BY t1.lastname
I'm desperate with this query. I have two tables table1 and table2, tables are identical but they have different data. I'm trying to remove duplicities by columns code and manufacturer. To do that I need in final result ID from table1 ID from table2 and also columns code and manufacturer
SELECT * FROM (
SELECT id,code,manufacturer FROM table1 WHERE manufacturer = 1
UNION SELECT id,code,manufacturer FROM table2 WHERE manufacturer = 1
) AS t GROUP BY code HAVING COUNT(*) > 1
But in result i got only values from table1. It's OK but I just need to get there id from table2 too. Please can anyone give me some tips how to do this ?
You have two basic problems:
Problem 1:
You are using UNION when you should be using UNION ALL, because UNION removes duplicates!
Problem 2:
This isn't the right way to go about the problem. You should be using a simple join, not a union.
Try this:
SELECT
t1.id as table1_id,
t2.id as table2_id,
t1.code,
t1.manufacturer
FROM table1 t1
JOIN table2 t2
ON t2.code = t1.code
AND t2.manufacturer = t1.manufacturer
WHERE manufacturer = 1 -- this WHERE clause is optional
Your use of the WHERE clause is a little odd - consider removing it to get all duplicates from all manufacturers.
Imagine I have table1 which has a column named 'table_name'. I use table1.table_name to store the name of another table in the database. The referenceable tables would all have a field 'target_id.
Is is possible to use table_name in a JOIN statement?
For example:
SELECT t1.*, t2.* FROM table1 AS t1
JOIN table1.table_name AS t2 ON t1.table1_id = t2.target_id
The obvious solution is to use the script (C++ in my case) to get the table name first, and construct a SQL query from it. The question is: can we bypass the script and do this directly in SQL (MySQL)?
Edit: What is dynamic SQL?
The only chance you have is to do 2 SQL statements:
select the tablename you need
use this table-name to dynamically build the secound query to get the data you need - what you want isn't possible to do with SQL directly (and it sounds like you've designed your database wrong in some way - but that's hard to say without knowing what's the goal of it).
I know I'm late to the party, but I wanted to offer a different solution. I see this sort of thing a lot in audit tables. The column table_name would refer to "what table was changed" and table1_id would refer to the ID of the row that changed in that table. In this case, the audit table is pointing back to many different tables that don't normally get joined.
Here goes:
SELECT t1.*, t2.*, t3.*, t4.*, t5.*
FROM table1 AS t1
left JOIN table2 AS t2
ON t1.table1_id = t2.target_id
and t1.table_name = 'table2'
left JOIN table3 AS t3
ON t1.table1_id = t3.target_id
and t1.table_name = 'table3'
left JOIN table4 AS t4
ON t1.table1_id = t4.target_id
and t1.table_name = 'table4'
left JOIN table5 AS t5
ON t1.table1_id = t5.target_id
and t1.table_name = 'table5'
Of course, the main drawback is that each table that can be possibly referenced needs to be explicitly included in the SQL command.
You can get more elegant output using this as your select list:
SELECT
t1.*,
coalesce(t2.fieldA, t3.fieldA, t4.fieldA, t5.fieldA) as fieldA,
coalesce(t2.fieldB, t3.fieldB, t4.fieldB, t5.fieldB) as fieldB
etc