I have two tables named :
url_alias
product
url_alias TABLE has following fields : id, query, keyword
id is numeric,
query is of the form "product_id=45"
keyword is of the form "product-actual-name-in-url-friendly-manner"
product table has following fields: product_id, name, language_id
What I want is to update url_alias table "keyword" field with proper url friendly string which I have generated by using mysql REPLACE function and is aliased as NEW_KEYWORD but the url friendly string needs to be auto-generated from a join of both tables .
Following query shows a SELECT query on tables properly:
SELECT u.url_alias_id, u.query, u.keyword, p.name, REPLACE( p.name, ' ', '-' ) AS NEW_KEYWORD
FROM url_alias u, product p
WHERE u.query = CONCAT( "product_id=", p.product_id )
AND p.language_id =3
Please help me in an update query by using this query
Try it:
UPDATE url_alias u
JOIN product p
ON u.query = CONCAT( "product_id=", p.product_id )
SET u.keyword=REPLACE( p.name, ' ', '-' )
WHERE p.language_id =3;
Related
I have more than 100 tables in SQL which don't have primary keys or indexes on the columns.
I'm manually checking each column for each table to see whether they have unique keys or not.
How do I query to get unique key columns if they are not present in the table?
Here You Need To Find Column Name Belongs To Particular Table:
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%write here column name%';
Please try following query
select stat.table_schema as database_name,
stat.table_name,
stat.index_name,
group_concat(stat.column_name
order by stat.seq_in_index separator ', ') as columns,
tco.constraint_type
from information_schema.statistics stat
join information_schema.table_constraints tco
on stat.table_schema = tco.table_schema
and stat.table_name = tco.table_name
and stat.index_name = tco.constraint_name
where stat.non_unique = 0
and stat.table_schema not in ('information_schema', 'sys',
'performance_schema', 'mysql')
and (tco.constraint_type !='UNIQUE' OR tco.constraint_type !='PRIMARY KEY') //You can made changes here if needed
group by stat.table_schema,
stat.table_name,
stat.index_name,
tco.constraint_type
order by stat.table_schema,
stat.table_name;
you may want to try something like this
SELECT schema_name(t.schema_id), t.name, i.name
FROM sys.indexes i
INNER JOIN sys.tables t ON t.object_id= i.object_id
WHERE i.type>0 and t.is_ms_shipped=0 and t.name<>'sysdiagrams'
and (is_unique_constraint=1)
Courtesy: This link
I have a query (below) which returns and ID value and two additional associated values (ReplacesReference). The query returns 2 records. I want to return one record so I need to concatenate the first "ReplacesReference" value with a comma ',' and the second "ReplacesReference" value.
my query is as follows
select
distinct(c.wiid) as ID,
a.reference as ReplacesReference
from npp_projects a,
npp_assoc b,
npp_projects c
where a.id = b.parent_id
and b.type = 'replace'
and c.id = b.child_id
and c.reference like '%EN 815%'
Its output is :
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996
CEN3406 | I.S. EN 815:2004
I want to be able to return the below output:
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996 , I.S. EN 815:2004
Many thanks in advance for your help - I am tearing my hair out with this one !
You want to use group by instead of select distinct. Your use of parentheses around c.wild suggests that you don't understand how select distinct works. It applies to all columns in the select list. The parentheses don't affect it. The query looks like:
select p2.wiid as ID,
group_concat(distinct p.reference separator ', ') as ReplacesReference
from npp_projects p join
npp_assoc a
on p.id = a.parent_id
npp_projects p2
on p2.id = a.child_id
where a.type = 'replace' and p2.reference like '%EN 815%'
group by p2.wild;
The changes I made:
Added the group_concat() to get the list you want.
Added the group by to replace the select distinct.
Changed the table aliases to use table abbreviations so the query is easier to follow.
Changed the join syntax to the more powerful explicit join syntax. As a simple rule: just do not use commas in the from clause.
You'd have to do something like:
SELECT id, GROUP_CONCAT(string SEPARATOR ' ') FROM table GROUP BY id;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
I have the following query:
SELECT
issue.`sequence` AS issue_sequence,
issue.`description` AS issue_description,
GROUP_CONCAT(DISTINCT(issue_category.`name`) SEPARATOR ', ') AS issue_category_name,
GROUP_CONCAT(DISTINCT(approach.`name`) SEPARATOR ', ') AS approach_name,
issue_approach.`issue_id` AS issue_approach_issue_id,
issue_approach.`approach_id` AS issue_approach_approach_id
FROM
`approach` approach
INNER JOIN `issue_approach` issue_approach ON approach.`id` = issue_approach.`approach_id`
INNER JOIN `issue` issue ON issue_approach.`issue_id` = issue.`id`
INNER JOIN `project` project ON issue.`project` = project.`id`
INNER JOIN `tenant` tenant ON project.`tenant_id` = tenant.`id`
INNER JOIN `issue_category` issue_category ON project.`id` = issue_category.`project`
INNER JOIN `user` user ON tenant.`id` = user.`tenant_id`
WHERE user.id = 1 AND project.id = 1
GROUP BY issue_category_name
ORDER BY issue_category.`name`, issue.`sequence`
I am having a problem with this line:
GROUP BY issue_category_name
Apparently, MySQL can't seem to Group by the alias for by GROUP_CONCAT result.
I am not really an expert with SQL, but is there a way I can group using the result of GROUP_CONCAT?
Sample data;
Categories: Network, Servers
Issue Id: 1
Description: Some description
Approaches: Some approaches to resolve the issue.
Basically, an issue can belong to one or many categories. I am concatenating categories for each issue. What i want to do is group the results by the result of concatenation of categories. So for example group issues whose categories are Network,Servers.
Thanks.
Im not a MySQL user, but change your group by to
Group By GROUP_CONCAT(DISTINCT(issue_category.`name`) SEPARATOR ', ')
With reference to SQL EXECUTION ORDER, the reason why this will not work is because the select statement is the last statement to be executed so that sql engine will not be knowing your column alias while grouping the records as GROUP BY occurs before SELECT. So that as Charles Bretana's answer suggests, put
Group By GROUP_CONCAT(DISTINCT(issue_category.`name`) SEPARATOR ', ')
in your group by clause. It will work fine then.
Hope this helps you.
I have tables contracts and users and i need to show contracts sorted by concatecated fields from users table. (and need outer join here because there is not always user present for contract)
SELECT *
FROM `contracts`
LEFT OUTER JOIN `users` ON `users`.id = `contracts`.account_manager_id
WHERE contracts.status != 'Archived'
ORDER BY CONCAT_WS(' ', IFNULL(`users.contact_first_name`, '')
, IFNULL(`users.contact_last_name`, ''))
LIMIT 0, 50
Problem: Unknown column users.contact_first_name
Remove the backticks here:
`users.contact_first_name`
Change to:
users.contact_first_name
Or if you really want to use backticks you can quote the table and column name separately:
`users`.`contact_first_name`
I have an SQL LIKE:
SELECT S.*,
(SELECT I.NAME FROM institution I, inst_map IM
WHERE IM.STUDENT = S.ID AND IM.INSTITUTION = I.ID) as INSTITUTIONS
FROM student S
In this case it is possible for my subquery to return multiple records (I will get an error: Subquery returns more than 1 row).
How to show those multiple values from my subquery in one field (in my case INSTITUTIONS ) separated by commas?
All ideas are welcome.
Try this query -
SELECT s.*, GROUP_CONCAT(t.NAME) INSTITUTIONS FROM student s
LEFT JOIN (SELECT * FROM institution i
JOIN inst_map im
ON im.INSTITUTION = i.ID
) t
ON s.ID = t.STUDENT
GROUP BY s.ID
The GROUP_CONCAT function will help you to get values separated by commas.
DECLARE #List VARCHAR(5000)
SELECT #List = COALESCE(#List + ', ' + Display, Display)
FROM TestTable
Order By Display
query is taken from following link, and the article explain the query perfectly, I hope it works
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/289/creating-comma-separated-list-in-sql.aspx
P.S Its for SQL Server, i guess