0

Below query is providing result of 22 rows in my sql

select pk.partnerid
     , pk.id as packageId
     , pk.name as package
     , p.partnername as partner
     , bp.name
     , bp.bookedDate
     , bp.email
     , bp.phone 
  from packages as pk 
  join bookpackage as bp 
    on bp.pkId = pk.id 
  left 
  join partner as p 
    on p.id = pk.partnerid 
 where pk.status > 0 
 order by (bookedDate is null) asc
     , bookedDate asc

where as the same in application provides only 3 rows which has not null dates

mysqli_query($con, "
select pk.partnerid
     , pk.id as packageId
     , pk.name as package
     , p.partnername as partner
     , bp.name
     , bp.bookedDate
     , bp.email
     , bp.phone 
  from packages pk 
  join bookpackage bp 
    on bp.pkId = pk.id 
  left 
  join partner p 
    on p.id = pk.partnerid 
 where pk.status > 0 
 order
    by (bookedDate is null) asc
     ,  bookedDate asc
");

I want to show all data and order by date if date is not null.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Ajimi Mol
  • 29
  • 4

3 Answers3

0

check bookedDate is not null in where condition

mysqli_query($con, "
select pk.partnerid
     , pk.id as packageId
     , pk.name as package
     , p.partnername as partner
     , bp.name
     , bp.bookedDate
     , bp.email
     , bp.phone 
  from packages pk 
  join bookpackage bp 
    on bp.pkId = pk.id 
  left 
  join partner p 
    on p.id = pk.partnerid 
 where pk.status > 0 
 and bookedDate IS NOT NULL
 order by bookedDate asc
");
Rp9
  • 1,955
  • 2
  • 23
  • 31
0

so if the date is null how it should be sorted? as I understand form your question, you have only 3 records that have then date, the other dates are null in case you need to sort those 3 then all the records, then you may choose 1 of the following solutions:

  • convert all null to 0 in your db if possible, you can also make the field as not null, and it will be added as 0 automatically if not inserted
  • you have an option in your query to replace null with 0, check this answer Replace null with 0 in MySQL
Bassem Shahin
  • 656
  • 7
  • 13
0

Have you tried

ORDER BY -bookedDate asc, bookedDate asc

instead of

order by (bookedDate is null) asc , bookedDate asc

pnbps
  • 67
  • 2
  • 6