I'm using PDO with MySQL.
I want to select all rows from a given table with distinct values in a given column, but SELECT DISTINCT column_name FROM table returns the rows with only that column_name. Therefore I can't access the other row's columns.
I've been searching for answers and it looks like SELECT DISTINCT column_name FROM table is supposed to return all the rows with distinct values inside column_name with all the row's columns. However, I only get the column I want distinc'ed:
Array
(
[image] => leather_helmet.jpg
// there are supposed to be more fields here...
)
May this be a PDO's bug or am I doing something wrong?
Thanks in advance! :)
If you want only 1 column distinct you have to think of which record you want for the other columns. For instance if you like the min id record for the distinct column then you can do
SELECT *
FROM armor_unsealed
WHERE id IN
(
SELECT min(id)
FROM armor_unsealed
WHERE piece=:piece
GROUP BY image
)'
Related
Is there any possible way I can find and set the column name by giving alias
for example
I have a sql queries which contain 4 column name fields. 3 fields are common in all the queries
id, name, field
and there is another field which column name get change every time but the only common thing in that field it has a postfix as __type
so my sql query looks like this
SELECT * from table_name
id, name, field, system_data__value
is there any possible way I can add alias to the name where I found __type as type
so if I run my queries then it look like this
SELECT * from table_name
id, name, field, type
You may use UNION ALL for to set the aliases to the columns posessionally.
You must know some value which cannot present in some column (id = -1 in shown code) for fake row removing.
SELECT *
FROM (
SELECT -1 id, NULL name, NULL field, NULL alias_for_column_4
UNION ALL
SELECT * from table_name -- returns id, name, field, system_data__value
) subquery
WHERE id > 0 -- removes fake row
It is possible that the values in fake subquery needs in explicit CAST() calls for correct datatype of the output columns setting.
I want to have a SELECT statement name columns based on other column values.
Let's say I have a table with column names like q_1, q_2 and other columns like q_1_name and q_2_name
Right now we are doing something like
SELECT SUM(q_1), SUM(q_2) from mytable;
I'd like to get a result set with the columns named for the values in q_1_name and q_2_name
SELECT SUM(q_1) as (q_1_name), SUM(q_2) as (q_2_name) from mytable;
Any chance you know a way to do this?
You can use a simply alias AS
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable;
or using a subselect
select t.q_1_name, t.q_2_name
from (
SELECT SUM(q_1) as q_1_name, SUM(q_2) as q_2_name from mytable
) t;
I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC
My query is like this
select 5 from mytable_name;
Then the output is like column name 5 and the value is 5 printing as many max number of rows exists in that table.
Can anybody tell the reason why this query is working like this?
Can anybody tell the reason why this query is working like this?
You are selecting a string literal value '5' for each row in your table:
select 5 from mytable_name;
And this works fine. Because in the SELECT statement you can select:
Column reference,
Literal value like in your case.
Function.
value expression.
Select expression.
As defined by the standard SQL1:
Update:
However, If you have a column with a name is a number like in your case, you have to escape it in order to select the values in it like so:
SELECT `143` FROM Table1;
This will select all the rows in the column 143.
But, this:
SELECT 143 FROM Table1;
Will select the string literal 143 for each row found in the table.
Note that: If possible, try not to name these columns this way, it is recommended and a best practice, not to do this.
SQL Fiddle Demo
Update 2:
Note that, if you select 143 or '143', or even "143" this will select the literal value 143 not the column date. The following are the same:
SELECT 143 FROM Table1;
SELECT '143' FROM Table1;
SELECT "143" FROM Table1;
All these SELECTs won't select the data in the column, They will select the literal value 143 not the column data. See this demo:
Demo
You have to escape the column name with the two:
``
Like this:
SELECT `143` FROM table1;
Not:
SELECT '143' FROM table1'
Like what I did here:
The right Demo
1Image From: SQL Queries for Mere Mortals
from mytable
will select all rows from your table if there is no where condition that shrinks that result. and
select 5
will select the constant number 5 for every record. If you use a column name in the select part then that value will be selected for every record.
The DB engine will name the result 5 because it automatically generates a column name and 5 is the logical name for that.
You want 'SELECT * FROM mytable_name LIMIT 0,5' perhaps?
Since you don't have anything in your where clause, it is selecting all the rows from your table. The fact that you don't select any of the columns is irrelevant - you'll still get a result for each row in the table.
With the command select 5 ... you are viewing a fixed value. Same thing you run the following command: select "test", you will be displaying a fixed string.
Using ... from mytable_name you're looking for all record of this table.
With this we can conclude that for each record in the table mytable_name shows you the fixed value "5".
I have the following select but it repeats the results of the second inner select 3 times.
Can anyone tell me why this is.
Also when I get the results how can I know which table the results came from. Home_content or facilities_table.
SELECT * FROM (SELECT hm_id, hm_name, hm_summary,
MATCH (hm_name, hm_summary) AGAINST ('test') AS score FROM home_content
WHERE MATCH (hm_name, hm_summary) AGAINST ('test') UNION SELECT fac_id,fac_name,
fac_summary, MATCH (fac_title, fac_summary) AGAINST ('test') AS score FROM
facilities_table WHERE MATCH (fac_title, fac_summary) AGAINST ('test')) a
ORDER BY SCORE DESC
Thanks in advance
Can't see why you are getting duplicates from this query unless:
There are duplicates in the source table
The same data appears in home_content and facilties_table (perhaps one is a view of the other?)
The second part of question as to deciding which table the contents of the union comes from is easily address by adding a constant column to each query of the union giving you something like this:
SELECT * FROM (SELECT 1,hm_id, hm_name, hm_summary,
MATCH (hm_name, hm_summary) AGAINST ('test') AS score FROM home_content
WHERE MATCH (hm_name, hm_summary) AGAINST ('test') UNION SELECT 2,fac_id,fac_name,
fac_summary, MATCH (fac_title, fac_summary) AGAINST ('test') AS score FROM
facilities_table WHERE MATCH (fac_title, fac_summary) AGAINST ('test')) a
ORDER BY SCORE DESC
In this case the initial column should be 1 for the home_content table and 2 for the facilities_table. Obviously string constants could be used in a similair style if that suited you better.