PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 1, 2022

[FIXED] MYSQL concat with sub query

 February 01, 2022     mysql, php, phpmyadmin, sql     No comments   

Issue

I need to concatenate the address fields (zip,city,countryName, streetAddress) of my table. This is the query i wrote,

SELECT id,concat_ws(' ',address1,zip,city,(select countryName from country where countryCode = User.countryCode))  FROM User 

And it provides me 28 Avenue Pasteur 14390 Cabourg France which should actually be, 27 Avenue Pasteur, 14390 Cabourg, France (with commas separated)

How can i do achieve this?

Update when used SELECT id,concat_ws(',',address1,zip,city,(select countryName from country where countryCode = User.countryCode)) FROM User

it gives 27 Avenue Pasteur, 14390, Cabourg, France

but not 27 Avenue Pasteur, 14390 Cabourg, France (no comman in between 14390 Cabourg)


Solution

Try this way (also, it is better to use join):

select u.id,concat(u.address1,', ',u.zip,' ',u.city,', ',c.countryName) as Address
from User u join
     country c on u.countryCode=c.countryCode

The result will be:

27 Avenue Pasteur, 14390 Cabourg, France


Answered By - Raging Bull
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing