mysql query sort database - mysql

Hello I have a table with 4 column in the column 4 the value come from a list box already pre establish by me , let say column 4 is populate by value a,b,c,d,e,f,g,h,i,j.
presently I can sort them in alpha order and get that on a PHP page .
I will like to be able to get on a page just d,e and f
how can I achieve that
thank you all

SELECT * FROM mytable WHERE mycolumn4 IN ('d','e','f')

You can just do:
SELECT column4
FROM TABLE
WHERE column4 IN ('d','e','f')
ORDER BY column4 ASC;
or if you know you want the elents with letters "bigger" than 'd' but don't know how many they are, you can do:
SELECT column4
FROM TABLE
WHERE column4 >= 'd'
ORDER BY column4 ASC;

Related

Order by first non-null result that comes from two different columns

I want to browse through all values of two columns in a table:
if the value in column 1 is not null, select it, otherwise select the value in column 2 instead.
then sort the final result in alphabetical ascending order, wherever column its values came from.
I tried the following query but it doesn't work and I'm not even sure it is supposed to do what I want to do.
SELECT *
FROM table
ORDER BY (CASE WHEN col1 IS NOT NULL THEN 1 ELSE 2 END ),
col1 DESC,
col2 DESC)
Besides the fact that it doesn't work (nothing outputted), it seems to sort the values of each column separately while I want to sort the final set of values retrieved, regardless of the column they are from.
Thank you for your help.
If you want to fix it with the CASE expression, it'd look like the following:
SELECT *,
CASE WHEN col1 IS NOT NULL
THEN col1
ELSE col2
END AS col
FROM table
ORDER BY col
Although a nice option is using the COALESCE function. It returns the first non-null value in the list of arguments.
SELECT *, COALESCE(col1, col2) AS col
FROM table
ORDER BY col

UNION not removing duplicates

I am trying to query 2 tables, return the values while combining duplicates. While UNION is supposed to remove duplicates, it seems to fail in this case. Note that this query is sent with PHP.
QUERY
<?php
$keywords = 'MOTOR';
$toSend = "SELECT part as Column1, '' as Column2 FROM soldParts WHERE part LIKE '%".$keywords."%' UNION SELECT part as Column1, vin as Column2 FROM vinData WHERE part LIKE '%".$keywords."%' ORDER BY column1 ASC";
?>
CURRENT OUTPUT
Column1 Column2
motor 1
motor 2
motor 2
CCDD44
motor 3
AABB1122
motor 3
DESIRED OUTPUT
Column1 Column2
motor 1
motor 2
CCDD44
motor 3
AABB1122
I have looked up several similar questions. However, my second column should just be merged (or concatenated), unlike other questions and/or they do not involve multiple tables. Also note that table soldParts Column2 has no value.
Your resultset actually has no duplicates. Duplicates are rows where all columns have equal values, and no row in your resultset complies to that definition.
Presumably, you want aggregation in the outer query:
select column1, max(column2) column2
from (
select part as column1, null as column2 from soldparts where part like ?
union all
select part as column1, vin as column2 from vindata where part like ?
)
group by column1
order by column1 asc
Note that I modified your query to use bind parameters (?); for the sake of security and efficiency, you should learn to use parameterized query rather than concatenating variables in the query string.
To get the result you want you'll need to perform an aggregation on top of the UNION (but before the ORDER BY).
For example:
select Column1, max(Column2) as Column2
from (
SELECT part as Column1, '' as Column2
FROM soldParts
WHERE part LIKE ?
UNION
SELECT part, vin
FROM vinData
WHERE part LIKE ?
) x
GROUP BY Column1
ORDER BY Column1

SQL Get last rows of different enum values

I do have a table with one "type"-column, this column is a enum with eight possibilities. I now want to query the last row of each enum value - so if I'm looking for four of the eight possible values, I want four rows with each one of the enums.
Any ideas?
Edit examples:
following enum: type=("example1","ex2","ex3")
Each row has a type and a timestamp
I'd like to have the last timestamp of every type.
Sample Data:
example 1 - 7:13,
ex2 - 8:14,
example 1 - 9:17,
ex3 - 6:13
Wished Result:
example 1 - 9:17,
ex2 - 8:14
Dialect is MySQL. Thanks!
If your table MyTable has an "enum" column named EnumColumn, and you want last row for each value in a list of given enum values (Foo, Bar, and Baz), where "last" is defined by a DateColumn, and you're using a database that supports windowing functions (e.g. MS SQL Server, Oracle, PostgreSQL), you can do it like this:
SELECT Column1, EnumColumn, Column3, DateColumn, Column5
FROM ( SELECT Column1, EnumColumn, Column3, DateColumn, Column5
, ROW_NUMBER() OVER ( PARTITION BY EnumColumn
ORDER BY DateColumn DESC ) AS ROW_NUM
FROM MyTable
WHERE EnumColumn IN ('Foo', 'Bar', 'Baz')
) x
WHERE ROW_NUM = 1

How to sum the records of Varchar2 and Null Values?

In my table I have same dates but with different Invoice_no_pawl varchar2 and Null. How to show the 1200.00 in the first place and of the date?
This is the query:
select invoice_date, invoice_no_pwal, invoice_value, payment_rec
from partywise_accounts_ledger
where name_of_the_customer = 'Naresh Agencies'
ORDER BY column1 DESC, column2
This sorts everything by column1 (descending) first, and then by column2 (ascending, which is the default) whenever the column1 fields for two rows are equal.
Best of luck!
Reference: Here

Want to choose something between count(*) or count(distinct *)

Let's say I have a table
with
Column1
A
A
A
B
B
C
C
C
Column2
CH
FH
FH
BW
CH
AW
Now: I want to have a select sth---> result 6 different combinations of column1 and column 2.
If i say select count(column2): I'll take 8.I don't want it.
If i say select(distinct column2): I'll take 5.I don't want it either.
The 6 different results I look for are: A-QW,A-CH,B-FH,C-BW,C-CH,C-AW
I'm looking for combinations.
Can you helpt me?
Use GROUP BY clause:
SELECT Column1,Column2 FROM tblname GROUP BY Column1,Column2
Grouping will do what you need. It will conveniently eliminate the duplicates for you.
select column1, column2
from <your_table>
group by column1, column2
Pretty sure this will do what you want. demo here
update based on comment
select count(*) from (
select column1, column2
from <your_table>
group by column1, column2
) q
updated demo
In some cases, we can use an expression which combines the two columns...
As an example:
SELECT COUNT(DISTINCT CONCAT(t.column1,'-',t.column2)) AS cnt_combinations
FROM mytable t
This only works in the special case, where we know that a dash character isn't included as a character in column1 or column2. Adding that delimiter character isn't strictly necessary either.
In the more general case, this wouldn't produce the "correct" results if the contents of the columns included values like.
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
A -CW A--CW
A- CW A--CW
Since the result of the expression is the same value, those won't be seen as distinct values.
The same thing happens with NULL values, because CONCAT() will return NULL whenever any of the argument values is NULL.
For example, using the string NULL to represent a NULL value
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
NULL CW NULL
A NULL NULL