简体   繁体   中英

Selecting only one column when using distinct

How can I select only the property_id from this query?

select distinct(property_name), property_id from prop where property_name 
like '%blah%';

I am trying to insert new rows to the table app_prop with the property_id exactly like the query above, so something similar to this:

INSERT INTO app_prop (function, PROPERTY_ID)
VALUES('dataq', select distinct(property_name), property_id from prop 
where property_name like '%blah%');
-- only want property_id included NOT property_name --

If you only want the property_id , then select only that.

Also: when using a select statement as a source for an insert statement, do not use the values clause.

To only insert distinct values for property_id you can use this:

INSERT INTO app_prop (function, PROPERTY_ID)
select distinct 'dataq', property_id 
from prop 
where property_name like '%blah%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM